2016-06-12 3 views
-4
print("Hello there") 
name=input("What is your name?") 
print("Welcome to the some game, " + name + "!") 
print("I'm going to ask you some basic questions so that we could work together") 
age=input("Your age") 
if age >= 14 and age < 41: 
    print("K") 
else: 
    print("Sorry bruh") 
print("Thanks") 

은 15시에 "Sorry bruh"가 계속 표시됩니다. 이유는 무엇입니까? 뭐가 문제 야?왜이 간단한 코드가 잘못 되었습니까?

+0

ussing 파이썬 3 나는 형식 오류'얻을 : unorderable 유형 : STR()> = INT()'내가 문제를 얻을 파이썬 2를 사용 . (그렇다면 문자열 이름을 사용할 수없는 다른 사람) –

+0

@ Tadhg, 오류는 없지만 잘못된 대답을 얻습니다. 파이썬 2는 호환되지 않는 타입의 _names_를 비교합니다! (''int "<"str "') – alexis

+0

@alexis no, 파이썬 2 파이썬 3에'K'를 출력하는 나이를 '15'로 입력하면 str과 비교하려고 할 때 에러가 발생합니다. int, 그래서 나는 ''을 표시 할 수 없으므로 '15'를 입력하면 끝에 "Sorry bruh"가 표시됩니다. '' ' –

답변

3

캐스트는 inputint에 :

age = int(input("Your age")) 

당신은 try-except를 추가 할 수 있습니다. 귀하의 조건은 동일하게 or하지 and

+0

너무 바보 같았습니다. 고맙습니다 – Metalnakls

0

어느 쪽이든 당신은 Python3 사용 평가와는 int(input("Your age"))와 함께 캐스팅 할 필요하거나 Python2를 사용하고 당신은 이름을 읽을 raw_input를 사용해야한다 : 당신, 그래서 name=raw_input("What is your name?")

0

확인을 python2.7에서이 코드를 시도 할 수 있습니다 당신이 원하는대로 작동합니다 :

print("Hello there") 
name=raw_input("What is your name?") 
print("Welcome to the some game, " + name + "!") 
print("I'm going to ask you some basic questions so that we could work together") 
age=input("Your age") 
if age >= 14 and age < 41: 
    print("K") 
else: 
    print("Sorry bruh") 
print("Thanks") 
관련 문제