简体   繁体   English

为什么我的代码不应该重复?

[英]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.

相关问题 为什么我的脚本先打开然后不应该立即关闭? (蟒蛇) - Why is my script is opening then closing immediately when it shouldn't? (Python) 为什么我的 Python 线程不应该启动? - Why does my Python thread start when it shouldn't? 为什么我的关联模型不应该在数据集中找到子组? - Why does my association model find subgroups in a dataset when there shouldn't any? 为什么我的正则表达式模式中的条件语句在不应该改变任何东西的情况下摆脱了其他匹配? - Why is the conditional statement in my regex pattern getting rid of other matches when it shouldn't change anything? 为什么我不应该将我的文件命名为模块的名称? - Why shouldn't I name my file the name of a module? 当我更改不应触发任何更新的按钮设置时,为什么我的 plot 会被面板更新(两次)? (面板霍洛维兹) - Why is my plot updated by panel (twice) when I change a button setting that shouldn't trigger any updates? (Panel Holoviz) 为什么Python不应该给我“需要一个整数”? - Why is Python giving me “an integer is required” when it shouldn't be? 为什么Python不应该返回False语句? - Why Python returns a False statement when it shouldn't? 程序在不应该关闭时关闭 - Program closing when it shouldn't pygame中不应有的运动 - Movement in pygame when there shouldn't be
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM