我要么没有使用正确的搜索字符串,要么深深埋藏在互联网中。我知道我们不应该要求做作业答案,但我不想要代码答案,我想知道在哪里找到它,因为我的GoogleFu被破坏了。
分配是创建一个程序,它将滚动两个6面骰子n
次,其中n
是用户定义的,介于1和9之间。然后程序显示结果,{{ 1}}如果滚动为1-1,如果滚动为6-6则为"Snake Eyes!"
。它还必须处理"Boxcar!"
(就像有人把“三”而不是“3”)并且如果用户选择的数字不是1-9的整数,则返回一条消息。
谢谢!
答案 0 :(得分:0)
您可以执行以下操作将最终输出存储到文本文件中。
def print_text(your_result):
with open('results.txt', 'w') as file:
file.write(your_result)
# Take users input
user_input = input("Do you want to save results? Yes or No")
if(user_input == "Yes"):
print_text(your_result)
我希望这会有所帮助
答案 1 :(得分:0)
答案 2 :(得分:0)
嗯,它不漂亮,但我想出了这个:
def print_text():
with open('results.txt', 'w') as file:
file.write(str(dice))
loop = True
import random
min = 1
max = 6
dice = []
while loop is True:
try:
rolls = int(input("How many times would you like to roll the dice? Enter a whole number between 1 and 9: "))
except ValueError:
print("Invalid option, please try again.")
else:
if 1 <= rolls <= 9:
n = 0
while n < rolls:
n = n + 1
print("Rolling the dice ...")
print("The values are:")
dice1 = random.randint(min, max)
dice2 = random.randint(min, max)
dice.append(dice1)
dice.append(dice2)
print(dice1, dice2)
diceTotal = dice1 + dice2
if diceTotal == 2:
print("Snake Eyes!")
elif diceTotal == 12:
print("Boxcar!")
else: print("Invalid option, please try again.")
saveTxt = input("Would you like to save as a text file? Y or N: ")
if saveTxt == "Y" or saveTxt == "y":
print_text()
break