我这辈子都无法修复我的无限循环
I cannot for the life of me fix my endless loop
我正在制作一个掷骰子来掷两个骰子并给出总数,一旦用户提示 n 再掷一次,它应该停止,我遇到的问题是脚本永远掷两个骰子而不提示选择 2,请帮助。
print('Dice Roller')
print()
choice1 = 'y'
choice2 = 'y'
choice1 = input('Roll the dice? (y/n): ')
while choice1 == 'y':
import random
possibleResults = [1, 2, 3, 4, 5, 6]
result1 = random.choice(possibleResults)
result2 = random.choice(possibleResults)
total = (result1 + result2)
print('Die 1: ', str(result1))
print('Die 2: ', str(result2))
print('Total: ', total)
print()
if (result1, result2) == 1:
print('Snake eyes!')
print()
elif (result1, result2) == 6:
print('Boxcars!')
print()
choice2 = print('Roll again? (y/n): ')
if choice2 == 'y':
continue
else:
break
两个主要问题是您的 ìf
语句的条件永远不会为真,然后您在要求输入的地方使用 print()
。
您可以将 if 的条件更改为:
if (result1, result2) == (1, 1):
或:
if result1 == 1 and result2 == 1:
而你再次要求选择的那一行,你应该把它改成:
choice2 = input('Roll again? (y/n): ')
就像你第一次要求输入一样。
在那之后它似乎对我有用,但你仍然只是问他们是否应该在你掷双 6 时继续。你可能想把它从 elif
中取出,所以它会在每次之后询问卷。您还使用了两个 choice
变量,然后使用 if
退出循环,但是因为您在循环条件中使用了第一个变量,所以您可以重复使用该变量并获得相同的效果。类似于:
print('Dice Roller')
print()
choice = 'y'
choice = input('Roll the dice? (y/n): ')
while choice == 'y':
import random
possibleResults = [1, 2, 3, 4, 5, 6]
result1 = random.choice(possibleResults)
result2 = random.choice(possibleResults)
total = result1 + result2
print('Die 1: ', str(result1))
print('Die 2: ', str(result2))
print('Total: ', total)
print()
if (result1, result2) == (1, 1):
print('Snake eyes!\n')
elif (result1, result2) == (6, 6):
print('Boxcars!\n')
choice = input('Roll again? (y/n): ')
这些条件永远不可能成立:
if (result1, result2) == 1:
print('Snake eyes!')
elif (result1, result2) == 6:
print('Boxcars!')
因为元组不能等于单个整数。相反,您可能想这样做:
if (result1, result2) == (1, 1):
print('Snake eyes!')
elif (result1, result2) == (6, 6):
print('Boxcars!')
或者等同于你可以做类似的事情:
if total == 2:
print('Snake eyes!')
elif total == 12:
print('Boxcars!')
另请注意,您在 boxcars 之后再次掷骰子的提示实际上并不是来自用户 input
。如果您只是获得该输入并将其简单地分配给您的 while
循环谓词所基于的变量(而不是有两个不同的变量,这是不必要的复杂),您不需要显式地 continue
或 break
循环。
其他几个小注意事项:我建议使用 random.randint
而不是硬编码列表中的 random.choice
,我还建议将结果放在一个元组中而不是必须在一堆地方将其重新构建为 result1, result2
。
import random
print('Dice Roller\n')
choice = input('Roll the dice? (y/n): ')
while choice == 'y':
results = random.randint(1, 6), random.randint(1, 6)
total = sum(results)
for i, result in enumerate(results, 1):
print(f'Die {i}: {result}')
print(f'Total: {total}\n')
if total == 2:
print('Snake eyes!\n')
elif total == 12:
print('Boxcars!\n')
choice = input('Roll again? (y/n): ')
我正在制作一个掷骰子来掷两个骰子并给出总数,一旦用户提示 n 再掷一次,它应该停止,我遇到的问题是脚本永远掷两个骰子而不提示选择 2,请帮助。
print('Dice Roller')
print()
choice1 = 'y'
choice2 = 'y'
choice1 = input('Roll the dice? (y/n): ')
while choice1 == 'y':
import random
possibleResults = [1, 2, 3, 4, 5, 6]
result1 = random.choice(possibleResults)
result2 = random.choice(possibleResults)
total = (result1 + result2)
print('Die 1: ', str(result1))
print('Die 2: ', str(result2))
print('Total: ', total)
print()
if (result1, result2) == 1:
print('Snake eyes!')
print()
elif (result1, result2) == 6:
print('Boxcars!')
print()
choice2 = print('Roll again? (y/n): ')
if choice2 == 'y':
continue
else:
break
两个主要问题是您的 ìf
语句的条件永远不会为真,然后您在要求输入的地方使用 print()
。
您可以将 if 的条件更改为:
if (result1, result2) == (1, 1):
或:
if result1 == 1 and result2 == 1:
而你再次要求选择的那一行,你应该把它改成:
choice2 = input('Roll again? (y/n): ')
就像你第一次要求输入一样。
在那之后它似乎对我有用,但你仍然只是问他们是否应该在你掷双 6 时继续。你可能想把它从 elif
中取出,所以它会在每次之后询问卷。您还使用了两个 choice
变量,然后使用 if
退出循环,但是因为您在循环条件中使用了第一个变量,所以您可以重复使用该变量并获得相同的效果。类似于:
print('Dice Roller')
print()
choice = 'y'
choice = input('Roll the dice? (y/n): ')
while choice == 'y':
import random
possibleResults = [1, 2, 3, 4, 5, 6]
result1 = random.choice(possibleResults)
result2 = random.choice(possibleResults)
total = result1 + result2
print('Die 1: ', str(result1))
print('Die 2: ', str(result2))
print('Total: ', total)
print()
if (result1, result2) == (1, 1):
print('Snake eyes!\n')
elif (result1, result2) == (6, 6):
print('Boxcars!\n')
choice = input('Roll again? (y/n): ')
这些条件永远不可能成立:
if (result1, result2) == 1:
print('Snake eyes!')
elif (result1, result2) == 6:
print('Boxcars!')
因为元组不能等于单个整数。相反,您可能想这样做:
if (result1, result2) == (1, 1):
print('Snake eyes!')
elif (result1, result2) == (6, 6):
print('Boxcars!')
或者等同于你可以做类似的事情:
if total == 2:
print('Snake eyes!')
elif total == 12:
print('Boxcars!')
另请注意,您在 boxcars 之后再次掷骰子的提示实际上并不是来自用户 input
。如果您只是获得该输入并将其简单地分配给您的 while
循环谓词所基于的变量(而不是有两个不同的变量,这是不必要的复杂),您不需要显式地 continue
或 break
循环。
其他几个小注意事项:我建议使用 random.randint
而不是硬编码列表中的 random.choice
,我还建议将结果放在一个元组中而不是必须在一堆地方将其重新构建为 result1, result2
。
import random
print('Dice Roller\n')
choice = input('Roll the dice? (y/n): ')
while choice == 'y':
results = random.randint(1, 6), random.randint(1, 6)
total = sum(results)
for i, result in enumerate(results, 1):
print(f'Die {i}: {result}')
print(f'Total: {total}\n')
if total == 2:
print('Snake eyes!\n')
elif total == 12:
print('Boxcars!\n')
choice = input('Roll again? (y/n): ')