繁体   English   中英

程序有什么问题

[英]What's is the wrong in the program

import random
random_Number = random.randrange(1, 4)

support = input('Write help to support ')

if support == "help":
    print("this is Rock, Paper, Scissors game ")
    play_Game = input('-enter 1 to Rock\n-enter 2 Paper\n-enter 3 to Scissors \n')
    if random_Number == 1 and play_Game == 1 :
        print('The game is draw\n Try Again!')

    elif random_Number == 2 and play_Game == 1:
        print('You lose!\n Hard luck')

    elif random_Number == 3 and play_Game == 1:
        print('You won!\n Good luck')

    elif random_Number == 1 and play_Game == 2:
        print('You won!\n Good luck')

    elif random_Number == 2 and play_Game == 2:
        print('The game is draw\n Try Again!')

    elif random_Number == 2 and play_Game == 2:
        print('The game is draw\n Try Again!')

    elif random_Number == 3 and play_Game == 2:
        print('You lose!\n Hard luck')

    elif random_Number == 1 and play_Game == 3:
        print('You lose!\n Hard luck')

    elif random_Number == 2 and play_Game == 3:
        print('You won!\n Good luck')

    elif  random_Number == 3 and play_Game == 3:
        print('The game is draw\n Try Again!')
    else:
        print('please, enter a correct number ')


else:
   print("write the true keyword")

评论:当我输入 1、2 或 3 时,它告诉我输入正确的数字,我认为错误在于 if 语句。 我学过python,这是我的第一个项目。 请告诉我这是一个干净的代码吗?

这是因为您正在将字符串整数进行比较。 在 python input()返回一个字符串。 当然,字符串永远不等于整数。

所以。 使用int()将输入转换为整数

play_Game=int(input('-enter 1 to Rock\n-enter 2 Paper\n-enter 3 to Scissors \n'))

在测试中保留字符串类型并用字符串替换数字可能更安全。 这样,如果在提示中输入的不是数字,程序就不会崩溃。

play_Game = input('-enter 1 to Rock\n-enter 2 Paper\n-enter 3 to Scissors \n')
if random_Number == "1" and play_Game == "1" :
    print('The game is draw\n Try Again!')

否则,任何非数字输入都会在转换为 int 期间触发错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM