猿问

我可以做些什么来修复我的简单电脑玩家游戏代码?语法错误

我是 Python 新手,我想练习循环,因为我遇到了最大的麻烦。我决定制作一个游戏,让用户从 0-100 中选择一个数字,看看他们是否能战胜电脑。我现在要做的只是开始。代码未完成。但是在尝试代码时,我得到了一个语法错误,箭头指向elif函数上的冒号。我该如何解决?我能做些什么?我接受对我的代码的任何其他附加评论,以使其更好。


这是我的代码:


import random

min = 0

max = 100

roll_again = "yes"

quit = "no"

players_choice = input()

computer = random.randint

while roll_again == "yes":

print("Pick a number between 1-100: ")

print(players_choice)

if players_choice >= 0:

print("Your number of choice was: ")

print(players_choice)

print("Your number is high.")

if computer >= 0:

print("Computers number is: ")

print(computer)

print("Computers number is high.")

if computer >= players_choice:

print("Computer wins.")

print("You lose.")

print("Would you like to play again? ", +roll_again)

elif: 

print(quit)

end

目标:


修复电脑玩家游戏,同时了解更多关于 python 的信息。提供有关从何处开始的其他文档会很有帮助。


holdtom
浏览 131回答 3
3回答

烙印99

你得到一个错误指向的原因elif是因为elif需要一个条件来检查。你需要像这样使用if elif和else:if a == b:    print('A equals B!')elif a == c:    print('A equals C!')else:   print('A equals nothing...')此外,Python 依靠缩进来确定什么属于什么,因此请确保您注意缩进(没有end)。修复 if 语句和缩进后,您的代码有更多错误,但您应该能够查找帮助来修复这些错误。

慕森王

有很多缩进问题,ifandelif语句使用不正确。还要看看 while 循环是如何工作的。根据您在此处提供的代码,这是一个可行的解决方案,但还有许多其他方法可以实现这一点。以下是一些关于if/else语句和其他初学者主题的有用教程:Python IF...ELIF...ELSE 语句import randomminval = 0maxval = 100roll_again = "yes"quit_string = "no"while True:&nbsp; &nbsp; players_choice = int(input("Pick a number between 1-100:\n"))&nbsp; &nbsp; computer = random.randint(minval,maxval)&nbsp; &nbsp; #checks if players choice is between 0 and 100&nbsp; &nbsp; if players_choice >= 0 and players_choice <= 100:&nbsp; &nbsp; &nbsp; &nbsp; print("Your number of choice was: ")&nbsp; &nbsp; &nbsp; &nbsp; print(players_choice)&nbsp; &nbsp; #otherwise it is out of range&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; print("Number out of range")&nbsp; &nbsp; #check if computers random number is in range from 0 to 100&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if computer >= 0 and computer <= 100:&nbsp; &nbsp; &nbsp; &nbsp; print("Computers number is: ")&nbsp; &nbsp; &nbsp; &nbsp; print(computer)&nbsp; &nbsp; # if computer's number is greater than players number, computer wins&nbsp; &nbsp;&nbsp; &nbsp; if computer > players_choice:&nbsp; &nbsp; &nbsp; &nbsp; print("Computer wins.")&nbsp; &nbsp; &nbsp; &nbsp; print("You lose.")&nbsp; &nbsp; #otherwise if players number is higher than computers, you win&nbsp; &nbsp; elif computer < players_choice:&nbsp; &nbsp; &nbsp; &nbsp; print("You won.")&nbsp; &nbsp; #if neither condition is true, it is a tie game&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; print("Tied game")&nbsp; &nbsp; #ask user if they want to continue&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; play_choice = input("Would you like to play again? Type yes or no\n")&nbsp; &nbsp; #checks text for yes or no use lower method to make sure if they type uppercase it matches string from roll_again or quit_string&nbsp;&nbsp; &nbsp; if play_choice.lower() == roll_again:&nbsp; &nbsp; &nbsp; &nbsp; #restarts loop&nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; elif play_choice.lower() == quit_string:&nbsp; &nbsp; &nbsp; &nbsp; #breaks out of loop-game over&nbsp; &nbsp; &nbsp; &nbsp; break

慕桂英3389331

你的代码有很多问题。这是一个工作版本,希望它可以帮助您理解一些概念。如果没有,请随时询问import random# min and max are names for functions in python. It is better to avoid using# them for variablesmin_value = 0max_value = 100# This will loop forever uless something 'breaks' the loopwhile True:&nbsp; # input will print the question, wait for an anwer and put it in the&nbsp; # 'player' variable (as a string, not a number)&nbsp; player = input("Pick a number between 1-100: ")&nbsp; # convert input to a number so we can compare it to the computer's value&nbsp; player = int(player)&nbsp; # randint is a function (it needs parentheses to work)&nbsp; computer = random.randint(min_value, max_value)&nbsp; # print what player and computer chose&nbsp; print("Your choice: ", player)&nbsp; print("Computer's choice: ", computer)&nbsp; # display the results&nbsp; if computer >= player:&nbsp; &nbsp; print("Computer wins. You loose")&nbsp; else:&nbsp; &nbsp; print("you win.")&nbsp; # Determine if user wants to continue playing&nbsp; choice = raw_input("Would you like to play again? (yes/No) ")&nbsp; if choice != 'yes':&nbsp; &nbsp; # if not&nbsp; &nbsp; break
随时随地看视频慕课网APP

相关分类

Python
我要回答