在python中执行while循环时返回none

时间:2017-03-06 17:31:29

标签: python syntax while-loop

在不获得相同数字的情况下获得最高分的游戏。这是在课堂上作为一项任务,当在while循环中运行时,你会得到打印位,然后在下一行None。请帮忙,因为我们的A-level课程中没有人也没有老师可以解决它出错的地方..

from random import randint
game = True
score = 0

die1 = randint(1,6)
die2 = randint(1,6)

while game == True:
    if die1 == die2:
        score = 0
        game = False
    else:
        score = score + die1 + die2
        goagain = input(print("do you want another go? Y or N"))
        if goagain == "N" or goagain == "n":
            game = False
        else:
            print("your current score is:" , score)

print("game over!! \nYour score is:" , score)

1 个答案:

答案 0 :(得分:1)

只需删除" print"从输入中删除无:

from random import randint
game = True
score = 0

die1 = randint(1,6)
die2 = randint(1,6)

while game == True:
    if die1 == die2:
        score = 0
        game = False
    else:
        score = score + die1 + die2
        goagain = input("do you want another go? Y or N")
        if goagain == "N" or goagain == "n":
            game = False
        else:
            print("your current score is:" , score)

print("game over!! \nYour score is:" , score)

但我不认为这是你想要的 - 你不会重新掷骰子,所以你只是不断地在分数上添加相同的数字直到用户停止。我想你想把模具卷放在循环中,如下所示:

from random import randint
game = True
score = 0

while game == True:
    die1 = randint(1,6)
    die2 = randint(1,6)
    if die1 == die2:
        score = 0
        game = False
    else:
        score = score + die1 + die2
        goagain = input("do you want another go? Y or N")
        if goagain == "N" or goagain == "n":
            game = False
        else:
            print("your current score is:" , score)

print("game over!! \nYour score is:" , score)

最后,在询问用户是否想要再次进行之前,你不打印出分数,这在第一次循环时似乎很奇怪。您可以将print语句移到else之外:

from random import randint
game = True
score = 0

while game == True:
    die1 = randint(1,6)
    die2 = randint(1,6)
    if die1 == die2:
        score = 0
        game = False
    else:
        score = score + die1 + die2
        print("your current score is:" , score)
        goagain = input("do you want another go? Y or N")
        if goagain == "N" or goagain == "n":
            game = False

print("game over!! \nYour score is:" , score)