Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.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 - Fatal编程技术网

Python 循环似乎从未真正循环过

Python 循环似乎从未真正循环过,python,Python,这就是我目前掌握的代码。然而,代码永远不会真正进入循环的开始,无论我输入什么,代码只会显示“感谢播放”并结束。谁能告诉我哪里出错了吗?首先,您应该使用原始输入来获取用户的选择。(假设python 2)如果您使用的是python 3,那么输入就可以了,请继续阅读 无论如何,当你输入yes时,它仍然会退出,因为你打破了循环!您应该将break语句移到“no”的情况下,这样当您说不想再次滚动时,它就会爆发 print("Welcome to my dice game.") print("First e

这就是我目前掌握的代码。然而,代码永远不会真正进入循环的开始,无论我输入什么,代码只会显示“感谢播放”并结束。谁能告诉我哪里出错了吗?

首先,您应该使用原始输入来获取用户的选择。(假设python 2)如果您使用的是python 3,那么输入就可以了,请继续阅读

无论如何,当你输入yes时,它仍然会退出,因为你打破了循环!您应该将break语句移到“no”的情况下,这样当您说不想再次滚动时,它就会爆发

print("Welcome to my dice game.")
print("First enter how many sides you would like your dice to have, 4, 6 or 12")
print("Then this program will randomly roll the dice and show a number")
#Introduction explaing what the game will do. Test 1 to see if it worked.
while True:
    #starts a while loop so the user can roll the dice as many times as they find necessary
    import random
    #Imports the random function so the code will be able to randomly select a number
    dice = int(input("Enter which dice you would to use,4, 6, or 12? "))
    #set a variable for the amount of dice number
    if dice == 12:
        x = random.randint(1,12)
        print("You picked a 12 sided dice. You rolled a " + str(x) + " well done")
        #Test 2 see if it does pick a random number for a 12 sided di
    elif dice == 6:
        x = random.randint(1,6)
        print("You picked a 6 sided dice. You rolled a " + str(x) + " well done")
        #Test 3 see if it does pick a random number for a 6 sided di
    elif dice == 4:
        x = random.randint(1,4)
        print("You picked a 4 sided dice. You rolled a " + str(x) + " well done")
        #Test 4 see if it does pick a random number for a 4 sided di
    else:
        print("Sorry, pick either 12, 6 or 4")
        #Test 5 tells the user that they can only pick 4, 6 or 12 if anything else is entered this error shows
    rollAgain = input ("Roll Again? ")
    if rollAgain == "no":
            rollAgain = False
    if rollAgain == "yes":
        rollAgain = True
        break
print ("Thank you for playing")
#if the user enters anything apart from yes y or Yes. The code ends here.

您根本不需要将rollReach设置为true或false。在上面的代码中,除“否”之外的任何内容都假定为“是”,但您可以轻松地添加检查。

问题在于,当用户想要再次掷骰子时,您打破了循环。当玩家不想再次玩时,循环应中断,因此您必须执行以下操作:


这种格式伤了我的眼睛。。还有我的感受。这是什么语言?如果用户选择“是”再次滚动,看起来你打破了循环。这看起来是相反的。双倍行距用于学期论文,而不是源代码。按照惯例,行注释在注释代码之前。这解决了问题。非常感谢。值得一提的是,如果在while循环之前将
rollreach
定义为
True
,则可以在rollreach时执行
而不是
while True
,然后在输入时执行
(“是否要再次滚动?”)。lower()。startswith('n')):rollreach=False
。仅供参考,使用pastebin或其他网站来承载代码不是一个好主意。学习使用密码,并在答案中输入密码。此外,您只需要修改的部分,而不需要全部。让您更容易看到不同之处。谢谢您的建议。你说得对,我会尽力遵守的。
rollAgain = raw_input ("Roll Again? ")
if rollAgain == "no":
    break