修复循环以继续程序,而不是在python中再次询问相同的问题
我对这个骰子游戏有问题。当我试图让它跳到下一段代码时,它只是返回。请帮助:修复循环以继续程序,而不是在python中再次询问相同的问题,python,python-3.x,Python,Python 3.x,我对这个骰子游戏有问题。当我试图让它跳到下一段代码时,它只是返回。请帮助: import random # Importing the module decision = ('y') # Setting variable 'decision' as y roll_number = 1 # Setting variable 'rolling_number' as 1 name = input('What is your name?\n') # Setting a variable as
import random # Importing the module
decision = ('y') # Setting variable 'decision' as y
roll_number = 1 # Setting variable 'rolling_number' as 1
name = input('What is your name?\n') # Setting a variable as a condition so that the User can change the variable. It is an input.
while decision == ('y'): # While loop for the decision
if roll_number > 1: # If loop for roll_number
same_person = input('\nAre you the same person? If so please press y, if not please press n. Pressing anything else ask your name again!\n') #Asking if the User is the same person
if same_person == ('y'): # If it is the same person
print('Okay!') # Outputs 'Okay!'
continue # Skips to the next part of the code
else: # Otherwise
name = input('\nWhat is your name?\n')#Asks name again
number = random.randint(1,6) # Generates random number between 1 and 6
ready = input('\nPress any button to roll the die!\n') # Lets the user know when it's ready
print('Rolling...\n'*4) # Output Rolling... 4 times
print(name,'! Your die shows a ',number,'!\n') # Outputs the name and the number
roll_number = roll_number+1 # Adds 1 to the variable 'roll_number'
decision = input('Do you want to try again? If so please press y, if not please press n. Pressing anything else will stop the program!\n') # Letting the User change the variable so they can use the program
else: # Otherwise
print('Bye!') # Outputs 'Bye!'
谢谢大家!
继续
只跳过循环体的其余部分,而不退出循环。同样,当same\u person=='y'
为true时,您将Python发送回,而decision=='y'
(注意,'y'
周围的括号在这里是完全多余的,可以省略)
使用break
退出循环。循环末尾的else
语句将不会执行,您可能需要删除那里的else:
并取消缩进print('Bye!')
行