![](/img/trans.png)
[英]Issue with OR statement in Python while making simple dice rolling game
[英]Dice rolling game in python
我對編碼和嘗試編寫一些擲骰子程序/游戲完全陌生。 我希望程序詢問用戶想要擲多少個骰子,以及他們可以“重試”在每場比賽中擲多少次骰子。 在用戶完成所有嘗試后,程序會跳回到第一個問題。
所以這就是我到目前為止所得到的:
import random
def roll(dice):
i = 0
while i < dice:
roll_result = random.randint(1, 6)
i += 1
print("You got:", roll_result)
def main():
rolling = True
while rolling:
answer = input("To throw the dice press Y if you want to stop press any key")
if answer.lower() == "Y" or answer.lower() == "y":
number_of_die = int(input("How many dice do you want to throw? "))
roll(number_of_die)
else:
print("Thank you for playing!")
break
main()
這有效,但我不確定從哪里開始讓程序詢問一名玩家每場比賽獲得多少次嘗試,以便如果他們對結果不滿意,他們可以再次滾動。 已經嘗試了一段時間來弄清楚,所以感謝任何提示!
這是一個更簡單的方法-
from random import randint
def roll(n):
for i in range(n):
print(f"You got {randint(1, 6)}")
def main():
tries = int(input("Enter the number of tries each player gets: "))
dice = int(input("How many dice do you want to roll? "))
for j in range(tries):
roll(dice)
roll_again = input("Enter 'y' to roll again or anything else to quit: ")
if roll_again.lower() != 'y':
break
main()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.