[英]Python dice roll game
I'm making a dice game on python and I'm having an issue.我正在 python 上玩骰子游戏,但遇到了问题。 "/roll1" and "/roll2" works perfectly but when I try "/score" it outputs nothing.
“/roll1”和“/roll2”工作得很好,但当我尝试“/score”时,它什么都不输出。 How can I fix this?
我怎样才能解决这个问题?
#Autharisation
# password is any integer
while True:
try:
pass_word = int(input("Please enter the password: "))
except ValueError:
print("Sorry, I didn't understand that.")
continue
else:
break
#Game
import random
def rolldie():
inputChoice = input("Enter Choice: ")
roll1 = ""
if(inputChoice == "/roll1"):
roll1 = random.randint(1,6)
print("Nice roll! You rolled a {}!".format(roll1) )
roll2 = ""
if(inputChoice == "/roll2"):
roll_2 = random.randint(1,6)
print("Nice roll! You rolled a {}!".format(roll_2) )
elif(inputChoice == "/score"):
print(roll1 + roll2 )
rolldie()
rolldie()
ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ
If you want to keep your function recursive, you have to pass your results from an execution to the next one, thanks to function parameters, for example:如果你想保持你的 function 递归,你必须将你的结果从一个执行传递到下一个执行,这要归功于 function 参数,例如:
import random
def rolldie(roll1=0, roll2=0):
inputChoice = input("Enter Choice: ")
if(inputChoice == "/roll1"):
roll1 = random.randint(1,6)
print("Nice roll! You rolled a {}!".format(roll1) )
if(inputChoice == "/roll2"):
roll2 = random.randint(1,6)
print("Nice roll! You rolled a {}!".format(roll2) )
if(inputChoice == "/score"):
print(roll1 + roll2)
rolldie(roll1, roll2)
rolldie()
You could also use a loop instead of the recursive way or use global variables but that is generally not the best option您也可以使用循环而不是递归方式或使用全局变量,但这通常不是最佳选择
I tried the Code and I think I found a solution to your problem, hope it helps, hears the code:我尝试了代码,我想我找到了解决您问题的方法,希望它能有所帮助,听到代码:
score1 = 0
score2 = 0
while True:
try:
pass_word = int(input("Please enter the password: "))
except ValueError:
print("Sorry, I didn't understand that.")
continue
else:
break
import random
def rolldie():
global score1, score
inputChoice = input("Enter Choice: ")
roll1 = 0
if(inputChoice == "/roll1"):
roll1 = random.randint(1,6)
score1 += roll1
print("Nice roll! You rolled a {}!".format(roll1) )
roll2 = 0
if(inputChoice == "/roll2"):
roll2 = random.randint(1,6)
score2 += roll2
print("Nice roll! You rolled a {}!".format(roll2) )
elif(inputChoice == "/score"):
print(score1 + score2)
rolldie()
rolldie()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.