大于或小于在 if 语句中不起作用的值
Greater than or less than values not working in if statement
我在处理一段测试代码时遇到了一些问题。我还是新手,如果让您感到困惑,我深表歉意。我试着做一个小骰子游戏,如果你得到更高的价值,你就会赢。但每次它只会说我赢了,我不知道为什么。如果很明显,我很抱歉,我似乎无法弄清楚。
mylist = {"Yes", "Sure", "yes", "sure"}
mylist2 = {"No", "Nope", "no", "nope"}
print("Dice game test")
def opponent():
opponent = random.randint(1,6)
return(f'He rolled a {opponent}')
o = opponent()
def player():
player = random.randint(1,6)
return(f'You rolled a {player}')
p = player()
answer = input("Ready to roll?")
if answer in mylist:
if (p > o):
print(f'{p} and {o}. You won, Test complete')
elif (o > p):
print(f'{p} and {o}. You lost, Test complete')
elif (p == o):
print(f'{p} and {o}. You got a tie, Test Complete')
else:
print("An error occurred")
else:
print("Wrong input")
基本上它似乎只告诉我我赢了,即使文本中显示的玩家数字小于对手也是如此。我只是想弄清楚如何让它正确显示数字并与输赢文本相匹配。
你的问题是你 return 一个字符串而不是随机整数。
您的代码应如下所示:
mylist = {"Yes", "Sure", "yes", "sure"}
mylist2 = {"No", "Nope", "no", "nope"}
print("Dice game test")
def getopponent():
opp = 4
print(f'He rolled a {opp}')
return opp
o = getopponent()
def getplayer():
player = 1
print(f'You rolled a {player}')
return player
p = getplayer()
answer = input("Ready to roll?")
if answer in mylist:
if (p > o):
print(f'{p} and {o}. You won, Test complete')
elif (o > p):
print(f'{p} and {o}. You lost, Test complete')
elif (p == o):
print(f'{p} and {o}. You got a tie, Test Complete')
else:
print("An error occurred")
else:
print("Wrong input")
我在处理一段测试代码时遇到了一些问题。我还是新手,如果让您感到困惑,我深表歉意。我试着做一个小骰子游戏,如果你得到更高的价值,你就会赢。但每次它只会说我赢了,我不知道为什么。如果很明显,我很抱歉,我似乎无法弄清楚。
mylist = {"Yes", "Sure", "yes", "sure"}
mylist2 = {"No", "Nope", "no", "nope"}
print("Dice game test")
def opponent():
opponent = random.randint(1,6)
return(f'He rolled a {opponent}')
o = opponent()
def player():
player = random.randint(1,6)
return(f'You rolled a {player}')
p = player()
answer = input("Ready to roll?")
if answer in mylist:
if (p > o):
print(f'{p} and {o}. You won, Test complete')
elif (o > p):
print(f'{p} and {o}. You lost, Test complete')
elif (p == o):
print(f'{p} and {o}. You got a tie, Test Complete')
else:
print("An error occurred")
else:
print("Wrong input")
基本上它似乎只告诉我我赢了,即使文本中显示的玩家数字小于对手也是如此。我只是想弄清楚如何让它正确显示数字并与输赢文本相匹配。
你的问题是你 return 一个字符串而不是随机整数。
您的代码应如下所示:
mylist = {"Yes", "Sure", "yes", "sure"}
mylist2 = {"No", "Nope", "no", "nope"}
print("Dice game test")
def getopponent():
opp = 4
print(f'He rolled a {opp}')
return opp
o = getopponent()
def getplayer():
player = 1
print(f'You rolled a {player}')
return player
p = getplayer()
answer = input("Ready to roll?")
if answer in mylist:
if (p > o):
print(f'{p} and {o}. You won, Test complete')
elif (o > p):
print(f'{p} and {o}. You lost, Test complete')
elif (p == o):
print(f'{p} and {o}. You got a tie, Test Complete')
else:
print("An error occurred")
else:
print("Wrong input")