[英]Why is my code repeating when it shouldn't?
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
num = int(input("Pick a number between 1-10 "))
while num != str(list):
print("That is not an adaquate choice.")
num = int(input("Pick a number between 1-10 "))
if num == 1:
file = open("external_files.txt", "w")
hsname = input("Please enter your name. If it is over 5 letters, use your initials. ")
if len(hsname) < 5:
print("Your name has been added to the scoreboard.")
file.write("\n")
file.write(hsname)
file.write(" - ")
file.write(str(x))
file.close()
else:
while len(hsname) > 5:
print("Sorry, that name is too long.")
hsname = input("Please enter your name. If it is over 5 letters, use your initials. ")
if len(hsname) < 5:
print("Your name has been added to the scoreboard.")
file.write("\n")
file.write(hsname)
file.write(" - ")
file.write(str(x))
file.close()
break
This is only a little bit of the code to not bore you with repetetiveness, but the loop does break under the right condidtions.这只是代码的一小部分,不会让您厌烦重复,但是在正确的条件下循环确实会中断。
My problem is that, further down in the code, it is mostly the same but without the while
and other statements that would deny it from making sense if I enter a number that is not in that list.我的问题是,在代码的更深处,它几乎是相同的,但没有while
和其他语句,如果我输入一个不在该列表中的数字,这些语句将否认它的意义。 But when I run it it contstantly says "That is not an adaquate choice."但是当我运行它时,它不断说“这不是一个合适的选择”。
str(list)
is returning the entire list as a single string which will never match the input. str(list)
将整个列表作为一个永远不会匹配输入的字符串返回。
Change this line:改变这一行:
while num != str(list):
To this:对此:
while not num in list:
As a side note, you should not use list
as a variable name since it is a type in Python.作为旁注,您不应该使用list
作为变量名,因为它是 Python 中的一种类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.