Как мне преобразовать числа в строке для завершения функции в Python 2.7?

#string #python-2.7 #int #typeerror #operands

#строка #python-2.7 #int #ошибка типа #операнды

Вопрос:

Я пытаюсь написать некоторый код, используя функции, if / elif и циклы. Я основываю это на трудном пути изучения Python, упражнение 35. (Python 2,7)

В настоящее время я застрял на временной функции def. Я не могу заставить программу принимать вводимые пользователем данные при вводе чисел.

Я получаю следующую ошибку:

 Traceback (most recent call last):
  File "ex35_1.py", line 53, in <module>
    temp ()
  File "ex35_1.py", line 11, in temp
    if number in next > 5:
TypeError: 'in <string>' requires string as left operand, not int

from sys import exit

def temp():
    print "Good morning."
    print "Let's get ready to kindergarden!"
    print "How cold is it outside?"

    #I think this is where the first problem is. 
        #The number-command is somehow wrong. 
    next = raw_input("> ")
    number = int(next)
    if number in next > 5:
        wool()
    elif number in next =< 6:
        not_wool()
    else:
        print "Fine, we just go!"

def wool():
    print "OK, so it is pretty cold outside!"
    print "Put on the wool."
    print "But is it raining?"
    rain = True

    while True:
        next = raw_input("> ")

        if next == "Yes":
            print "Put on the rain coat!"
            rain()
        elif next == "No" and rain:
            print "It is raining, but I dont wanna stress with the rain coat!"
            rain = False
        elif next == "No":
            print "You dont need a raincoat."
            march("With wool and no raincoat.")
        else:
            print "You should make a choice."
            exit(0)


def march(wit):
    print wit, "You are out the door!"
    exit (0)

def rain():
    print "Got the wellis?"
    march("With wool and rain stuff!")

def not_wool():
    print "There is no need for all that clothing."
    march("Remember the lunch pack!")

temp ()
  

Любые советы по упомянутой ошибке и вероятным другим ошибкам будут оценены.

Ответ №1:

Поскольку число уже является целым числом, вы можете сравнить его напрямую.

 number > 5
  

Ответ №2:

Вы преобразовали рядом с int и присвоили его переменной number. Затем вы пытаетесь найти это значение int в исходной строке.

Зачем вам нужно искать значение int в строке? Разве вы не можете просто оценить значение int?

Вы также можете вырезать строку, преобразовав входные данные в значение int при назначении его next.

Также =< должно быть <=

 def temp():
    print "Good morning."
    print "Let's get ready to kindergarden!"
    print "How cold is it outside?"

    next = int(raw_input("> "))

    if next  > 5:
        wool()
    elif next <= 6:
        not_wool()
    else:
        print "Fine, we just go!"