gpt4 book ai didi

Python:要求用户输入5个不同的标记

转载 作者:行者123 更新时间:2023-12-01 05:26:34 25 4
gpt4 key购买 nike

问题是编写一个程序,要求用户输入 5 个不同的学生以及他们的分数(满分 100 分)。如果用户尝试输入一个学生两次,程序应该检测到这一点并要求他们输入唯一的学生姓名 (和他们的标记)。

我的程序是..

dictionary = {}

count = 0

while count < 5:
name = raw_input("Enter your name: ")
mark = input("Enter your mark out of 100: ")
if name not in dictionary:
dictionary[name] = mark
count = count + 1
else:
name = raw_input("Enter a unique name: ")
mark = input("Enter the mark out of 100: ")
if name not in dictionary:
dictionary[name] = mark
count = count + 1

print dictionary

我的问题是,如果用户不断输入相同的名称和标记,如何循环 else: 代码?

最佳答案

你混合了inputraw_input,这是一件坏事。通常,您在 Python 2 中使用 raw_input ,在 Python 3 中使用 input 。解决问题的快速而肮脏的方法是:

dictionary = {}

count = 0

while count < 5:
name = raw_input("Enter your name: ")
mark = raw_input("Enter your mark out of 100: ")
if name not in dictionary:
dictionary[name] = mark
count = count + 1
else:
print("You already used that name, enter an unique name.")

print dictionary

关于Python:要求用户输入5个不同的标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21222989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com