Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我的计数变量不显示用户输入的数字?python_Python_Variables - Fatal编程技术网

为什么我的计数变量不显示用户输入的数字?python

为什么我的计数变量不显示用户输入的数字?python,python,variables,Python,Variables,我的python程序要求用户输入任意随机数,将它们存储在适当的变量中,然后在final语句中显示它们。我不知道为什么实际变量不显示计数的输入。我被困住了,困惑了 countOne = 0 countTwo = 0 countThree = 0 countFour = 0 countFive = 0 countSix = 0 print("Welcome") print("This program is determines how often a certain number app

我的python程序要求用户输入任意随机数,将它们存储在适当的变量中,然后在final语句中显示它们。我不知道为什么实际变量不显示计数的输入。我被困住了,困惑了

countOne = 0
countTwo = 0
countThree = 0
countFour = 0
countFive = 0
countSix = 0    


print("Welcome")
print("This program is determines how often a certain number appears after the dice have been tossed")
print("You will toss your dice then input the number rolled. After you have entered in the information, the program will tell you how many times the number appears")
userInput=int(input("Please enter the number shown(1-6) or enter 0 to quit: "))



for i in range(1, 6):
    if userInput == 1:
        countOne = countOne + 1
    elif userInput == 2:
        countTwo = countTwo + 1
    elif userInput == 3:
        countThree = countThree + 1
    elif userInput == 4:
        countFour = countFour + 1
    elif userInput == 5:
        countFive = countFive + 1
    elif userInput == 6:
        countSix = countSix + 1

while userInput != 0:
    if userInput >= 0 and userInput <= 6:
        userInput=int(input("Please enter the number shown(1-6) or enter 0 to quit: "))
    else:
        userInput=int(input("ERROR, NUMBER OUTSIDE RANGE!! Please enter the number shown(1-6) or enter 0 to quit: "))
print("Number 1 appeared: ",countOne," time.")
print("Number 2 appeared: ",countTwo," time.")
print("Number 3 appeared: ",countThree," time.")
print("Number 4 appeared: ",countFour," time.")
print("Number 5 appeared: ",countFive," time.")
print("Number 6 appeared: ",countSix," time.")
print("Thank you! Good Bye!")
countOne=0
count2=0
countThree=0
countFour=0
countFive=0
countSix=0
打印(“欢迎”)
打印(“此程序用于确定掷骰子后某个数字出现的频率”)
打印(“您将掷骰子,然后输入掷骰的数字。输入信息后,程序将告诉您数字出现的次数”)
userInput=int(输入(“请输入显示的数字(1-6)或输入0以退出:”)
对于范围(1,6)内的i:
如果userInput==1:
countOne=countOne+1
elif userInput==2:
countTwo=countTwo+1
elif userInput==3:
countThree=countThree+1
elif userInput==4:
countFour=countFour+1
elif userInput==5:
countFive=countFive+1
elif userInput==6:
countSix=countSix+1
当用户输入时!=0:

如果userInput>=0且userInput您的循环没有意义:

userInput=int(input("Please enter the number shown(1-6) or enter 0 to quit: "))
for i in range(1, 6):
    ...
while userInput != 0:
    ...
您接受一次输入,然后将同一输入计数五次,然后接受更多输入(不计数)。此外,有六个独立的变量是不必要的;只需使用六个整数的列表

尝试将输入验证分离为一个单独的函数(参见示例),然后执行以下操作:

counts = [0 for _ in range(6)]
while True:
    ui = get_input() # input function validates 0 to 6
    if ui == 0:
        break
    else:
        counts[ui-1] += 1 # note -1 - list is zero-based
请注意,使用列表可删除大量重复:

print("Number 1 appeared: ",countOne," time.")
print("Number 2 appeared: ",countTwo," time.")
print("Number 3 appeared: ",countThree," time.")
print("Number 4 appeared: ",countFour," time.")
print("Number 5 appeared: ",countFive," time.")
print("Number 6 appeared: ",countSix," time.")
变成:

for num, count in enumerate(counts, 1):
    print("Number {0} appeared: {1} time.".format(num, count))

它应该统计用户输入的任何随机数。例如,我的上一组输入有4个3,但只显示0个3。我不认为我可以从这段代码中提取太多的内容,使它仍然可以在没有语法错误的情况下工作。所以,我不认为最小化它会大大减少它。我在列表方面很糟糕,我试图通过创建多个变量使自己变得更容易。好的,我明白你在说什么,我将尝试重新做这个。谢谢你的帮助。