我有一个运行Craps游戏的简单程序。掷骰子游戏的所有逻辑都在playOnce()方法中。我需要使用一个调用playOnce()方法的main()方法,以确定用户是否想通过输入yes或no再次播放。我需要经常询问用户他们是否想在每轮比赛后进行比赛。我在实现询问用户的那条逻辑时遇到了麻烦。我尝试过使用while循环,但到目前为止还没有输出。这是一个示例:http://tinypic.com/r/2las1v/8
import random
def playOnce():
dice1 = random.randint(1, 6)
dice2 = random.randint(1, 6)
roll = dice1 + dice2
print("You rolled", dice1, "+", dice2, "=", roll)
print()
if roll == 2 or roll == 3 or roll == 12:
print("You lose!")
elif roll == 7 or roll == 11:
print("You win!")
else:
print("Point is", roll)
dice1 = random.randint(1, 6)
dice2 = random.randint(1, 6)
roll2 = dice1 + dice2
print("You rolled ",roll2)
while roll2 != 7:
if roll == roll2:
print("You win!")
break
else:
print("Point is ", roll)
dice1 = random.randint(1, 6)
dice2 = random.randint(1, 6)
roll2 = dice1 + dice2
print("You rolled ",roll2)
if roll2 == 7:
print("You lose!")
def main():
playOnce()
print("Would you like to go again?")
main()
答案 0 :(得分:1)
怎么样:
def main():
keep_playing = True
while keep_playing:
playOnce()
ans = raw_input("Would you like to go again?")
if ans != 'y':
keep_playing = False
答案 1 :(得分:0)
您可以使用:
import random
def playOnce():
dice1 = random.randint(1, 6)
dice2 = random.randint(1, 6)
roll = dice1 + dice2
print("You rolled", dice1, "+", dice2, "=", roll)
print()
if roll == 2 or roll == 3 or roll == 12:
print("You lose!")
elif roll == 7 or roll == 11:
print("You win!")
else:
print("Point is", roll)
dice1 = random.randint(1, 6)
dice2 = random.randint(1, 6)
roll2 = dice1 + dice2
print("You rolled ",roll2)
while roll2 != 7:
if roll == roll2:
print("You win!")
break
else:
print("Point is ", roll)
dice1 = random.randint(1, 6)
dice2 = random.randint(1, 6)
roll2 = dice1 + dice2
print("You rolled ",roll2)
if roll2 == 7:
print("You lose!")
def main():
playOnce()
#print("Would you like to go again?")
flag = input("Would you like to go again?")
#flag = True
while flag:
playOnce()
flag = input("Would you like to go again?")
main()
然后你需要回复真或假或1或0