输出我不想要的东西

问题描述 投票:1回答:1

我真的很新。我学习Python已有5-6天了,并且正在做一些简单的小项目。好吧,我有一个项目,其中有一个随机生成的数字。我输入了一个数字,它告诉我是否猜到了随机生成的数字。代码很糟糕,但是我不知道太多,所以我设法做到了。

这是我的代码:

# The idea is that when you enter a number 0-20 it shows you if you guessed the number or no.
import random
computer_number = str(random.randint(0, 20))
person_number = str(input("Please insert a random number 0-20: "))

##############

if person_number != computer_number:
    if person_number > computer_number:
        if person_number > str(20):
            print()
            print("You entered a number that's higher than the possible.")
        print()
        print("Your number is too high! The computer chose " + computer_number)
    elif person_number < computer_number:
        if person_number < str(0):
            print()
            print("You entered a number that's lower than the possible.")
        print()
        print("Your number is too low! The computer chose " + computer_number)
    else:
        print()
        print("You entered an invalid option.")

##############

elif person_number == computer_number:
    print()
    print("You guessed the number! Congratulations")

input("""
Press ENTER to close the program.""")

我也很累,所以我可能看不到问题,但是无论我输入什么数字,它总是显示这两个字符串:

You entered a number that's higher than the possible.
Your number is too high! The computer chose " + computer_number

谢谢!

python output
1个回答
-1
投票

尝试一下,它应该可以工作:

import random
computer_number = (random.randint(0, 20))
person_number = int(input("Please insert a random number 0-20: "))

##############

if person_number != computer_number:
    if person_number > computer_number:
        if person_number > (20):
            print()
            print("You entered a number that's higher than the possible.")
        print()
        print("Your number is too high! The computer chose " + str(computer_number))
    elif person_number < computer_number:
        if person_number < (0):
            print()
            print("You entered a number that's lower than the possible.")
        print()
        print("Your number is too low! The computer chose " + str(computer_number))
    else:
        print()
        print("You entered an invalid option.")

##############

elif person_number == computer_number:
    print()
    print("You guessed the number! Congratulations")

input("""Press ENTER to close the program.""")

只需更改代码即可在“ int”中进行数字之间的比较。

© www.soinside.com 2019 - 2024. All rights reserved.