嗯,这是我的代码
import time
import random
Dice = input("Would you like to roll the dice?(y/n)")
if Dice == "y":
roll = random.randint(1,6)
print("You have rolled a", roll)
again = input("Would you like to roll again?(y/n)")
while again == "y":
roll2 = random.randint(1,6)
print("You have rolled a", roll2)
again = input("Roll again?(y/n)")
if again == "n":
time.sleep(1)
print("Goodbye")
else:
time.sleep(1)
print("Goodbye")
如果你能提供帮助,那就太好了!
答案 0 :(得分:4)
import random
while input('RTD? (y/n) ') == 'y': print('Rolled {}.'.format(random.randint(1, 6)))
答案 1 :(得分:2)
我觉得做某人的功课。但是你去了,你的代码的缩短版本(未经测试)。
import time
import random
dice = input("Would you like to roll the dice?(y/n)")
while dice == "y":
print( "You have rolled a %s" % (random.randint(1,6)))
dice = input("would you like to roll again?(y/n)")
time.sleep(1)
print("Goodbye")
答案 2 :(得分:0)
import time
import random
while input("Would you like to roll the dice? (y/n) ") == 'y':
roll = random.randint(1,6)
print("You have rolled a", roll)
print("Goodbye")
time.sleep(1)
答案 3 :(得分:0)
使用while循环:
import time
import random
Dice = raw_input("Would you like to roll the dice?(y/n)")
while Dice.lower() == "y":
roll = random.randint(1, 6)
print("You have rolled a ", roll)
Dice = raw_input("Would you like to roll again?(y/n)")
time.sleep(1)
print("Goodbye")