[英]Dice and gamble game Python
您好,我的代碼有問題。 我想在方法 main 中檢查 ran_dice 的值,但我不知道該怎么做。 例如,我寫了 ran_dice(2) 它返回 2 個隨機整數,我想檢查這兩個整數是否相等。 我可以在 main 方法中做嗎? 如何 ?
打印ran_dice(2)
應該可以解決問題。
根據評論編輯:
a,b=ran_dice(2)
if a==b:
# code to stop
然而,正如另一條評論所提到的, ran_dice(s)
函數有點危險,因為它返回的東西數量各不相同。 讓程序返回一致數量的東西是一種很好的做法。 您可以返回列表中的值,並且列表的大小可能會有所不同,但至少您總是返回一個列表。
這是一個示例,您可以在列表中返回骰子值。 所以你可以隨意調整返回的骰子數量,並比較結果。
import random
def ran_dice(s):
if s==1:
a=random.randint(1,6)
return [a]
elif s==2:
a=random.randint(1,6)
b=random.randint(1,6)
return [a,b]
def main():
credit=100
print('Welcome user, you have ', credit,'credits.')
number=int(input('How much do you want to gamble?: '))
while number <0 or number>100:
print('You need to give a positive integer no more than your credit.')
number=int(input('How much do you want to gamble?: '))
result = ran_dice(2)
print ("dice=", result)
firstdice = 0
for dice in result:
if firstdice == 0:
firstdice = dice
elif dice == firstdice:
print("equal")
else:
print("different")
if result[0] == result[1]:
print("equal")
main()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.