为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

holdtom 2022-04-23 21:25:44
我是 Python 新手,我想练习循环,因为我遇到了最大的麻烦。我决定制作一个游戏,让用户从 0-100 中选择一个数字,看看他们是否能战胜电脑。我现在要做的只是开始。代码未完成。但是在尝试代码时,我得到了一个语法错误,箭头指向elif函数上的冒号。我该如何解决?我能做些什么?我接受对我的代码的任何其他附加评论,以使其更好。这是我的代码:import randommin = 0max = 100roll_again = "yes"quit = "no"players_choice = input()computer = random.randintwhile 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 的信息。提供有关从何处开始的其他文档会很有帮助。
查看完整描述

3 回答

?
烙印99

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 语句和缩进后,您的代码有更多错误,但您应该能够查找帮助来修复这些错误。


查看完整回答
反对 回复 2022-04-23
?
慕森王

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


查看完整回答
反对 回复 2022-04-23
?
慕桂英3389331

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


查看完整回答
反对 回复 2022-04-23
  • 3 回答
  • 0 关注
  • 130 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信