3 回答
TA贡献1831条经验 获得超4个赞
您应该修改 cpu_choice 函数以返回一个值。此外,还应删除您的 return 语句,如其旁边的评论中所述:
def cpu_choice(): #This function is for the computer to get a option.
list_option = [1 , 2, 3]
cpu = random.choice(list_option)
#return(random.choice(list_option)) #This returns a number, but in your compare code you are comparing strings, so take this line out
if cpu == 1:
cpu = "Rock"
if cpu == 2:
cpu = 'Paper'
if cpu == 3:
cpu = 'Scissor'
return cpu
在主函数中,您可以将另一个名为 cpu 的变量设置为名为 cpu_choice 的函数的返回值
def main(cpu,get): #Executes the programs and checks to see if the input is valid.
print('Rock = 1')
print('Paper = 2')
print('Scissor = 3')
again = 'y'
while get < 1:
get = int(input('Enter a valid number.'))
while get > 3:
get= int(input('Enter a valid number.'))
if get == 1:
get = "Rock"
if get == 2:
get = 'Paper'
if get == 3:
get = 'Scissor'
cpu = cpu_choice()
compare(cpu,get)
TA贡献1841条经验 获得超3个赞
您的 cpu_choice 应如下所示:
def cpu_choice():#This function is for the computer to get a option.
list_option = [1 , 2, 3]
cpu = random.choice(list_option)
if cpu == 1:
cpu = "Rock"
if cpu == 2:
cpu = 'Paper'
if cpu == 3:
cpu = 'Scissor'
return cpu
这是因为 return 将退出函数,因此函数中 return 语句后面的任何代码将永远不会被执行。
您的主要功能应如下所示:
def main(cpu,get):# Executes the programs and checks to see if the input is valid.
print('Rock = 1')
print('Paper = 2')
print('Scissor = 3')
again = 'y'
while get < 1:
get = int(input('Enter a valid number.'))
while get > 3:
get= int(input('Enter a valid number.'))
if get == 1:
get = "Rock"
if get == 2:
get = 'Paper'
if get == 3:
get = 'Scissor'
compare(cpu,get)
您不需要在主函数中声明 cpu,因为您已经将 cpu 传递给了主函数。
在您的功能之外,您将需要这个:
get = int(input('Enter a number 1 to 3 as your choice.\n'))
cpu = cpu_choice()
main(cpu,get)
现在您的 main 函数具有它需要的所有参数。注意我放在get = int(input('Enter a number 1 to 3 as your choice.\n'))你的函数声明之后。这是一种常见的做法,可以让您更轻松地理解您的代码。
质量优化
Pythonrandom可以从列表中选择一个随机元素:
Or's 可elif用于在您赢时为 1,如果您输了则为 1。
考虑到您main()从内部调用compare()最好main()没有参数,而是在主函数中获取get和cpu
一个while语句可以有多个比较。
优化后的代码如下所示:
import random
def cpu_choice():
list_option = ["Rock" , "Paper", "Scissor"]
cpu = random.choice(list_option)
return cpu
def compare(cpu,get):
if get == cpu:
print('Its a tie!')
again = input('Enter Y or y to play again. Enter N or n to quit.')
if again == 'y' or again == 'Y':
main()
elif cpu == 'Rock' and get == 'Scissor' or cpu == 'Paper' and get == 'Rock' or cpu == 'Scissor' and get == 'Paper':
print('You lose.')
again = input('Enter Y or y to play again. Enter N or n to quit.')
if again == 'y' or again == 'Y':
main()
elif cpu == 'Rock' and get == 'Paper' or cpu == 'Paper' and get == 'Scissor' or cpu == 'Scissor' and get == 'Rock':
print('You win!')
again = input('Enter Y or y to play again. Enter N or n to quit.')
if again == 'y' or again == 'Y':
main()
def main():
print('Rock = 1')
print('Paper = 2')
print('Scissor = 3')
get = int(input('Enter a number 1 to 3 as your choice.\n'))
cpu = cpu_choice()
while not 4 > get > 0:
get = int(input('Enter a valid number.'))
if get == 1:
get = "Rock"
if get == 2:
get = 'Paper'
if get == 3:
get = 'Scissor'
compare(cpu,get)
main()
TA贡献1816条经验 获得超4个赞
在底部,您使用参数“cpu”(未定义)和“get”(由顶部的用户输入定义)调用 main。该程序接受 1 个输入并打印一个输出 - 您不需要将 cpu 参数提供给 main,因为它是从 cpu_choice 函数返回的内容生成的。只需将其作为参数删除并在调用 compare 之前写入 cpu = cpu_choice() ,并让 cpu_choice() 返回 cpu 值。
添加回答
举报