2017-11-17 5 views
0

필자는 필자의 요구를 충족시키는 대답을 찾지 못했거나 실제로 이해하기에 충분히 간단합니다.사용중인 변수를 사용자가 입력 한 것으로 변경하려고 시도했습니다.

난이도가 가변적 인 변수가있어서 사용자에게 문자열을 입력하도록 요청하여 쉽게 매체 또는 하드 모드를 원하는지 여부를 나타냅니다. 불행히도 성공적으로 파이썬 사용 된 단어에 대한 입력을 확인하고 내가 원하는 것을 제공 할 수 없습니다, 나는 쉽게 "정의되지 않았습니다"또는 "매체가 정의되지 않았습니다"또는 "하드 정의되지 않았습니다." 이 문제를 해결할 수있는 방법이 있습니까? 여기에 문제가 동봉되어 내 코드의 작은 부분이다 :

difficulty=input("What difficulty do you wish to choose, easy,medium or hard?") 
    if difficulty==easy: 
     print("You have chosen the easy mode, your test will now begin") 
     print("") 

    elif difficulty==medium: 
     print("You have chosen the medium mode, your test will now begin") 

    else: 
     print("You have chosen the hard mode or tried to be funny, your test 
     will now begin") 
+1

입력을 문자열과 비교해야합니다. 'difficult == "easy":' – Prune

+0

' 어려움 == "easy"'. 여기에 문자열 리터럴이 필요합니다. –

+1

@Jan'raw_input'이 버전 3의 파이썬에서 삭제되었습니다. –

답변

0

우선 들여 쓰기를 수정하십시오 (예제에만 문제가없는 경우). 둘째, 단일 ' 또는 더블 " 따옴표 중 하나에 쉽고, 중간 및 하드를 넣어해야합니다

difficulty=input("What difficulty do you wish to choose, easy,medium or hard?") 
if difficulty=="easy": 
    print("You have chosen the easy mode, your test will now begin") 
    print("") 

elif difficulty=="medium": 
    print("You have chosen the medium mode, your test will now begin") 

else: 
    print("You have chosen the hard mode or tried to be funny, your test 
    will now begin") 

따옴표에 넣어하지 않는 경우에, 당신은 쉽게 단어 difficulty을 비교하지 않습니다, 오히려 easy이라는 변수에 전달됩니다. 물론 변수가 없기 때문에 물론 오류가 발생합니다. 따옴표는 파이썬이 쉽게 문자열을 해석하도록 알려줍니다.

1

당신은 다음 (input 문자열을 반환합니다)이 경우에 다른 문자열과 비교 사용자로부터 문자열을 얻기 위해 노력하고있다는 'easy' 또는 'medium'. Here is a link to google dev 기사에서 파이썬으로 문자열에 할 수있는 기본적인 것들에 대해 이야기합니다.

difficulty = input("What difficulty do you wish to choose, easy,medium or hard?") 
if difficulty == 'easy': 
    print("You have chosen the easy mode, your test will now begin") 
    print("") 

elif difficulty == 'medium': 
    print("You have chosen the medium mode, your test will now begin") 

else: 
    print("You have chosen the hard mode or tried to be funny, your test will now begin") 

당신은 그들이 변수 (link to python variable tutorial) 것을 당신이 파이썬을 말하고있다 코드에서 easy 또는 medium을 넣지 문자열. 이 경우 easy 변수를 아직 정의하지 않았습니다 (예 : easy = 'some data'). 당신이 그것을 정의하지 않았기 때문에 파이썬은 easy으로 무엇을 해야할지 모른다. 이것은 오류를 던질 것이다.