2014-11-16 5 views
0

Наличие некоторых проблем с этим кодом. Когда я ввожу 2, чтобы попробовать и выполнить whaleq(), вместо этого он выполняет mathschallenge(), который я хочу выполнить только тогда, когда пользователь вводит 1.определение переменных и использование переменной python

Может ли кто-нибудь взглянуть и помочь мне исправить это? Код ниже точно такой же, как и код, который у меня есть на моем простоях python.

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

После взятия значения «localpick» от пользователя были вы, вызывая функцию «localx». –

+0

Надеюсь, это не ваш точный код. Это не с отступом. – RvdK

+0

изменил localx на localpick, и он ничего не сделал. И @RvdK мне пришлось вставить код, а затем вставить его здесь с четырьмя пробелами. так что он может быть выключен –

ответ

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

ПРИМЕЧАНИЕ: Это было проверено в питон 3.4.2

Смежные вопросы