3 回答
TA贡献1829条经验 获得超13个赞
你得到一个错误指向的原因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 语句和缩进后,您的代码有更多错误,但您应该能够查找帮助来修复这些错误。
TA贡献1777条经验 获得超3个赞
有很多缩进问题,ifandelif语句使用不正确。还要看看 while 循环是如何工作的。
根据您在此处提供的代码,这是一个可行的解决方案,但还有许多其他方法可以实现这一点。
以下是一些关于if/else语句和其他初学者主题的有用教程:
Python IF...ELIF...ELSE 语句
import random
minval = 0
maxval = 100
roll_again = "yes"
quit_string = "no"
while True:
players_choice = int(input("Pick a number between 1-100:\n"))
computer = random.randint(minval,maxval)
#checks if players choice is between 0 and 100
if players_choice >= 0 and players_choice <= 100:
print("Your number of choice was: ")
print(players_choice)
#otherwise it is out of range
else:
print("Number out of range")
#check if computers random number is in range from 0 to 100
if computer >= 0 and computer <= 100:
print("Computers number is: ")
print(computer)
# if computer's number is greater than players number, computer wins
if computer > players_choice:
print("Computer wins.")
print("You lose.")
#otherwise if players number is higher than computers, you win
elif computer < players_choice:
print("You won.")
#if neither condition is true, it is a tie game
else:
print("Tied game")
#ask user if they want to continue
play_choice = input("Would you like to play again? Type yes or no\n")
#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
if play_choice.lower() == roll_again:
#restarts loop
continue
elif play_choice.lower() == quit_string:
#breaks out of loop-game over
break
TA贡献2036条经验 获得超8个赞
你的代码有很多问题。这是一个工作版本,希望它可以帮助您理解一些概念。如果没有,请随时询问
import random
# min and max are names for functions in python. It is better to avoid using
# them for variables
min_value = 0
max_value = 100
# This will loop forever uless something 'breaks' the loop
while True:
# input will print the question, wait for an anwer and put it in the
# 'player' variable (as a string, not a number)
player = input("Pick a number between 1-100: ")
# convert input to a number so we can compare it to the computer's value
player = int(player)
# randint is a function (it needs parentheses to work)
computer = random.randint(min_value, max_value)
# print what player and computer chose
print("Your choice: ", player)
print("Computer's choice: ", computer)
# display the results
if computer >= player:
print("Computer wins. You loose")
else:
print("you win.")
# Determine if user wants to continue playing
choice = raw_input("Would you like to play again? (yes/No) ")
if choice != 'yes':
# if not
break
添加回答
举报