如何将str转换为int

时间:2019-07-27 12:55:10

标签: python lexicographic

我正在制作一个猜谜游戏,我需要知道如何阻止它按字典顺序排列答案,因为这会导致我的游戏出现错误。

我尝试过将elif Guess < str(value):制作为elif Guess < int(value):,但是我得到了错误消息“'<<,在'str'和'int'的实例之间不支持”,并且我正在没有其他的运气

这是我正在使用的代码

from random import *
from random import randint
import os

numbers = []
Guesses = []
dif = []

os.system('CLS')
print("I want you to guess my number")
print("\n \n \n \nIf you give up type 'give up' word for word")

f = open("Data.txt", "a")

print("Say 'yes' If You're Ready")

YN = input()



if YN == "yes":
    os.system('cls')
    print("Select a difficulty")
    print('impossible 1 - 10000')
    print('annoying 1 - 1000')
    print('hard 1 - 500')
    print('medium 1 - 200')
    print('easy 1 - 99')
    print('beginner 1 - 10')

diff = input()





if diff == 'easy':
    os.system('CLS')
    value = randint(1, 99)
    numbers.append(value)
    dif.append(diff)

elif diff == 'beginner':
    os.system('CLS')
    value = randint(1, 10)
    numbers.append(value)
    dif.append(diff)

elif diff == 'medium':
    os.system('CLS')
    value = randint(1, 199)
    numbers.append(value)
    dif.append(diff)

elif diff == 'hard':
    os.system('CLS')
    value = randint(1, 499)
    numbers.append(value)
    dif.append(diff)

elif diff == 'annoying':
    os.system('CLS')
    value = randint(1, 1000)
    numbers.append(value)
    dif.append(diff)

elif diff == 'impossible':
    os.system('CLS')
    value = randint(1, 10000)
    numbers.append(value)
    dif.append(diff)


os.system('cls')

while True:
    Guess = input()



    if Guess == "give up":
        print("The Number Was " + str(numbers))
        f.write("------------------------------------------------------------- \n \r")
        f.write("Guesses " + str(Guesses) + "\n \r")
        f.write("Difficulty: " + str(dif) + "\n \r")
        f.write("[USER GAVE UP] \n \r")
        f.write("Correct Answer: " + str(numbers) + "\n \r")
        f.write("------------------------------------------------------------- \n \r")
        break

    elif Guess < str(value):
        print("Higher")
        Guesses.append(Guess + " - Higher")

    elif Guess > str(value):
        print("Lower")
        Guesses.append(Guess + " - Lower")


    elif Guess == str(value):
        os.system('CLS')
        length = len(Guesses)
        f.write("------------------------------------------------------------- \n \r")
        f.write("Guesses " + str(Guesses) + "\n \r")
        f.write("Difficulty: " + str(dif) + "\n \r")
        f.write("Number Of Guesses [" + str(length) + "]\n \r")
        f.write("Correct Answer: " + str(numbers) + "\n \r")
        f.write("------------------------------------------------------------- \n \r")
        print("That Was Correct!")
        for x in Guesses:
            print(x)
        break

input()


2 个答案:

答案 0 :(得分:1)

您可以尝试将Guess从str转换为int,然后进行整数比较。

elif int(Guess) < value:

答案 1 :(得分:0)

正如maruchen指出的那样,您应该转换Guess变量而不是value变量。

但是,仅此一项仍会给您带来麻烦。用户没有被迫输入整数,并且确实希望得到诸如“放弃”之类的字符串答案。如果您尝试将字符串强制转换为整数,并且该字符串包含非数字,那么您将面临ValueError。所以最好是定义一个这样的函数:

def guess_less_than_value(guess, value):
    try:
        return int(guess) < int(value)
    except ValueError:
        return False

然后您可以但在代码正文中:

if guess_less_than_value(Guess, value):
    ....

同样大于。