2014-11-16 2 views
0

이 코드에 약간의 문제가 있습니다. 내가 2를 입력하여 whaleq()을 실행하려고하면, 대신에 mathschallenge()을 실행합니다. 사용자가 1을 입력 할 때만 실행되기를 원합니다.변수를 정의하고 변수 파이썬을 추출합니다

누구든지 모양을 고칠 수 있습니까? 아래 코드는 파이썬 유휴 상태의 코드와 완전히 동일합니다.

print('The first thing you should think about doing is asking the locals if they have seen  anything!') 

locallist = ['Ann-Marie, the butcher (1)', 
      'Bella, the flourist (2)', 
      'Caitlyn, the painter (3)', 
      'Daniel, the scientist (4)', 
      'Lauren, the Zookeeper (5)'] 
print(locallist) 

localpick = int(input('Type the number of the local you want to talk to ')) 

def localx(): 
    if localpick == 1: 
     mathschallenge() 
    if localpick == 2: 
     whaleq() 

def mathschallenge(): 
    print('maths challenge yolo') 
    quest = input('What is 10+10?') 
    if quest == '20': 
     print('correct') 
    else: 
     print('incorrect') 

def whaleq(): 
    mammal = input('What is the largest mammal?') 
    if mammal == 'whale': 
     print('correct') 
    else: 
     print('incorrect') 

input('Press the enter key to exit') 
+0

에서 테스트되었습니다. –

+0

정확한 코드가 아니기를 바랍니다. 그것은 들여 쓰기가 적절하지 않습니다. – RvdK

+0

localx를 localpick으로 변경했는데 다른 점이 없습니다. 그리고 @RvdK는 4 개의 공백으로 여기에 들여 쓰는 코드를 붙여 넣어야했습니다. 그래서 꺼져있을 수도 있습니다. –

답변

0

코드는 조금 재구성이 아닌 나를 위해 잘 작동, 나는 이동 :

print('The first thing you should think about doing is asking the locals if they have seen  anything!') 

locallist = ['Ann-Marie, the butcher (1)', 'Bella, the flourist (2)', 'Caitlyn, the 
painter   (3)', 'Daniel, the scientist (4)', 'Lauren, the Zookeeper (5)'] 
print(locallist) 

localpick = int(input('Type the number of the local you want to talk to ')) 
하단에 코드의 상단에서

, 바로 위의 localx()를 추가 :

input('Press the enter key to exit') 

전체 코드는 다음과 같습니다.

def localx(): 
    if localpick == 1: 
     mathschallenge() 
    if localpick == 2: 
     whaleq() 

def mathschallenge(): 
    print('maths challenge yolo') 
    quest = input('What is 10+10?') 
    if quest == '20': 
     print('correct') 
    else: 
     print('incorrect') 

def whaleq(): 
    mammal = input('What is the largest mammal?') 
    if mammal == 'whale': 
     print('correct') 
    else: 
     print('incorrect') 


print('The first thing you should think about doing is asking the locals if they have seen  anything!') 

locallist = ['Ann-Marie, the butcher (1)', 'Bella, the flourist (2)', 'Caitlyn, the painter  (3)', 'Daniel, the scientist (4)', 'Lauren, the Zookeeper (5)'] 
print(locallist) 

localpick = int(input('Type the number of the local you want to talk to ')) 
localx() 
input('Press the enter key to exit') 
나는 희망이 도움이

, ~ Bobbeh

참고 :이 말은 '한 localX'함수를 호출하고 사용자로부터 'localpick'값을 복용 한 후 파이썬 3.4.2

관련 문제