[英]Im a little stuck in a dice rolling program in python, am very new to python and very confused
這就是指令告訴我要做的…… main – 這個 function 是主程序。 它應該執行以下操作:詢問用戶他們想要擲多少個骰子。 如果用戶輸入一個小於 1 或大於 10 的數字,程序應該繼續詢問用戶一個介於 1 和 10 之間的有效數字。請記住,您可以使用 while 循環來進行輸入驗證。 一旦程序從用戶那里得到一個有效的數字,它應該在調用 roll_dice function 時使用該數字作為參數。
roll_dice - 這個 function 有一個參數 num_dice,它是要擲骰子的數量。 由於程序將顯示骰子的總數,因此首先將變量初始化為 0,這將保持運行總數。 使用 for 循環將每個骰子滾動指定的次數。 使用 random 模塊中的 randint function 獲得 1 到 6 之間的隨機數字。 打印示例 Output 中所示的循環迭代次數和模具值。 確保更新 for 循環內的運行總計。 在 for 循環完成后,打印所有擲出的骰子的總值。
這是我到目前為止的代碼:
import random
def main():
num_dice = int(input('How many dice do you want to roll?'))
while num_dice < 1 or num_dice > 10:
print('Enter a number between 1 and 10.')
num_dice = (input('How many dice do you want to roll?'))
roll_dice(num_dice)
def roll_dice(num_dice):
rolls = 0
for i in range(num_dice):
print(f'Roll #', rolls, random.randint(1, 6))
rolls+=1
main()
當我不斷收到此錯誤時,我的代碼顯然有問題:說錯誤消息
有一些改進:
:=
)就像是:
import random
def main():
# you can loop 'forever' and then break out under the right condition
while True:
# you'd forgotten one of the int()
num_dice = int(input('How many dice do you want to roll?'))
if num_dice < 1 or num_dice > 10:
print('Enter a number between 1 and 10.')
else:
break
result = roll_dice(num_dice)
print(f'The total for {num_dice} rolls was {result}.')
def roll_dice(num_dice):
rolls = 0
for i in range(1, num_dice + 1):
# you printed rolls, but you want i, starting at 1
# roll := something, causes roll to be assigned, but also returns the value
print(f'Roll #', i, roll := random.randint(1, 6))
rolls += roll
return rolls
main()
def main():
num_dice = int(input('How many dice do you want to roll?'))
while num_dice < 1 or num_dice > 10:
print('Enter a number between 1 and 10.')
num_dice = (input('How many dice do you want to roll?'))
roll_dice(num_dice)
def roll_dice(num_dice):
rolls = 0
for i in range(num_dice):
print(f'Roll #', rolls, random.randint(1, 6))
out_ran_num = random.randint(1, 6)
rolls+=out_ran_num
return rolls
主要的()
請檢查縮進
首先,我們想不斷詢問用戶他們想要擲多少個骰子,直到他們給出有效的輸入,我們可以使用 while 循環來做到這一點,如下所示:
valid_integer = False
# While the user hasn't given a valid integer
while valid_integer == False:
# Note: this will crash if the user does not input a number
num_dice = int(input('How many dice do you want to roll?'))
# Checking if the input is valid
if num_dice < 1 or num_dice > 10:
print("Please enter a number between 1 and 10")
# Because we did not change the variable valid_integer, it will loop again
else:
valid_integer = True
print(roll_dice(num_dice))
現在我們需要創建roll_dice
function,它需要一個參數(擲骰子的數量)並返回一個integer(擲骰子的總數)
import random
# num_dice: int is known as type hinting, it is used to indicate the type of a value
def roll_dice(num_dice: int):
total = 0
for i in range(num_dice):
roll = random.randint(1, 6)
total += roll
return total
現在我們可以把代碼放在一起了! 我們需要將循環放入 function main()
並調用它。 不要忘記你的進口!
import random
def main():
valid_integer = False
# While the user hasn't given a valid integer
while valid_integer == False:
# Note: this will crash if the user does not input a number
num_dice = int(input('How many dice do you want to roll?'))
# Checking if the input is valid
if num_dice < 1 or num_dice > 10:
print("Please enter a number between 1 and 10")
# Because we did not change the variable valid_integer, it will loop again
else:
valid_integer = True
print(roll_dice(num_dice))
# num_dice: int is known as type hinting, it is used to indicate the type of a value
def roll_dice(num_dice: int):
total = 0
for i in range(num_dice):
roll = random.randint(1, 6)
total += roll
return total
if __name__ == "__main__":
main()
有關if __name__ == "__main__"
== "__main__": 做什么?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.