Python 如果语句不执行
这是我在python中做过的第一件事,所以我知道代码效率很低,而且没有任何问题,但是任何指针都会被欣赏!基本上,我得到一个响应,就好像没有执行任何if语句,而found=False在整个程序中都是如此!因此,即使我通过调试知道MyChoice和aiChoice是有效的而不是绘图,输出仍然是“You draw with your antighter”Python 如果语句不执行,python,python-3.x,Python,Python 3.x,这是我在python中做过的第一件事,所以我知道代码效率很低,而且没有任何问题,但是任何指针都会被欣赏!基本上,我得到一个响应,就好像没有执行任何if语句,而found=False在整个程序中都是如此!因此,即使我通过调试知道MyChoice和aiChoice是有效的而不是绘图,输出仍然是“You draw with your antighter” import time as t import random as r import os os.system('@Color 0a') aiW
import time as t
import random as r
import os
os.system('@Color 0a')
aiWins = 0
MyWins = 0
Rock = 1
Paper = 2
Scissors = 3
found = False
#welcome text
print("\nWelcome to rock paper scissors")
t.sleep(1)
print("\nPlease enter a username")
user = input("> ")
def aiCheck(aiWins):
if aiWins > 5:
print("Unfortunately the computer has bested you this time! Try again.")
def myCheck(MyWins):
if MyWins > 5:
print("Congratulations you have won the game!")
def whowon(found, MyChoice, aiChoice, myWins, aiWins):
print (MyChoice)
print (aiChoice)
if MyChoice == 1 and aiChoice == 3:
found = True
t.sleep(2)
print('You chose rock and your opponent chose scissors! You win!')
MyWins = MyWins + 1
elif MyChoice == 2 and aiChoice == 1:
found = True
t.sleep(2)
print('You chose paper and your opponent chose rock! You win!')
MyWins = MyWins + 1
elif MyChoice == 3 and aiChoice == 2:
found = True
t.sleep(2)
print ('You chose scissors and your opponent chose paper! You win!')
MyWins = MyWins + 1
elif MyChoice == 3 and aiChoice == 1:
found = True
t.sleep(2)
print('You chose scissors and your opponent chose rock! You lose!')
aiWins = aiWins + 1
elif MyChoice == 1 and aiChoice == 2:
found = True
t.sleep(2)
print('You chose rock and your opponent chose paper! You lose!')
aiWins = aiWins + 1
elif MyChoice == 2 and aiChoice == 3:
found = True
t.sleep(2)
print ('You chose paper and your opponent chose scissors! You lose!')
aiWins = aiWins + 1
if found == False:
print("You drew with your opponent")
return found
return MyWins
return aiWins
print("\nOptions!")
t.sleep(1)
print('\n1. Rock')
print('2. Paper')
print('3. Scissors')
print('\nEnter the number that correlates with your choice')
MyChoice = input('> ')
aiChoice = r.randint(1,3)
whowon(found, MyChoice, aiChoice, MyWins, aiWins)
这将解决您的问题:
MyChoice = int(input('> '))
您正在比较字符串(MyChoice)和整数(aiChoice)。
input
返回一个字符串,因此必须使用整数转换器包装MyString
,如下所示:
MyChoice = int(input("> "))
由于无法将字符串与整数进行精确比较,found
未设置为True,因此found
为False,导致它报告平局
接下来,您不能使用单独的return语句返回多个内容,在本例中,因为您不需要对返回值执行任何操作,所以不需要返回多个内容。如果确实要返回值,可以使用元组返回:
return (found, MyWins, aiWins)
注意:参数名称不必与全局变量相同。参数变量是作为实际传入内容的占位符的局部变量。您还具有冗余参数。不需要传递found、MyChoice和aiChoice 首先,这些已经是全局变量了。不需要将它们用作参数
aiWins = 0
MyWins = 0
found = False
现在,您可以像这样定义方法,并使用global
关键字来确保您使用的是那些全局变量
def whowon(MyChoice, aiChoice):
global aiWins
global MyWins
global found
print (MyChoice)
print (aiChoice)
# etc...
然后,这些返回语句实际上并不需要。加上任何函数都以第一个返回语句结束
最后,input()
返回一个字符串,因此if语句将整数与字符串进行比较,这是false,因此它们按预期执行
要解决此问题,需要在比较之前将输入转换为整数。您可以直接在输入法上这样做
MyChoice = int(input("> "))
或者直接在参数上
whowon(int(MyChoice), aiChoice)
或函数中if语句中的每个变量。由您决定一个函数中不能有多个返回语句,就像int(MyChoice)会为您将choice转换为整数一样。这是一个写的字符串