2016-12-21 2 views
0

私は入力として1を入力します。私たちはハンバーガー、フライドポテト、ガンを持っていると言いたいのです しかし、 。どのように私はそれが私が欲しいものを出力するようにすることができますか?このコードをPython 3.5.2に入力すると、私が望むものが出力されません

print ("Hello welcome to Mcdonald's how may I help you") 
    print ("Type 1 for food") 
    print ("Type 2 for drinks") 
    user_input = input ("Type 3 for money") 
    if (user_input) == 1: 
     print ("We have Hamburgers, fries, and cancer") 
     print ("Type 1 for hamburgers") 
     print ("Type 2 for fries") 
     user_input = input ("Type 3 cancer") 
     if (user_input) == 1: 
      print ("That will cost you $1.25") 
      print ("Type 1 to pay") 
      user_input = input ("Type 2 to steal") 
      if (user_input) == 1: 
       user_input = input ("Thank you for comming") 
      elif (user_input) == 2: 
       user_input = input ("HEY COME BACK AND PAY!") 
     elif (user_input) == 2: 
      user_input = input ("Potato!") 
     elif (user_input) == 3: 
      user_input = input ("Sorry we are out of cancer") 
    if (user_input) == 2: 
     print ("We have Coke, and Pepsi") 
     print ("Type 1 for Coke") 
     user_input = input ("Type 2 for Pepsi") 
     if (user_input) == 1: 
      user_input = input ("COKE SUCKS NO DRINKS FOR YOU!!!!!") 
     elif (user_input) == 2: 
      print ("Nice I LOVE Pepsi Coke sucks that will cost you $1.00") 
      print ("Type 1 to pay") 
      user_input = input ("Type 2 to steal") 
      if (user_input) == 2: 
       user_input = input ("HEY COME BACK AND PAY!") 
    if (user_input) == 3: 
     print ("So heres the plan you take the register ill destract the others.") 
     print ("Type 1 To call the cops") 
     user_input = input ("Type 2 to do the plan") 
     if (user_input) == 1: 
      user_input = input ("You got $150 for calling the cops") 
     elif (user_input) == 2: 
      user_input = input ("You got $150 for doing the plan") 
    else: 
     print ("Sorry I did not get your order") 
    raw_input("Press Enter To Exit") 

答えて

0

トップレベルのifsにはelifが必要です。

elif (user_input) == 2: 
.... 
elif (user_input) == 3: 
..... 

あなたが唯一の四つのいずれかを実行するように、あなたのコードは、1または2と他のエラー、または

3んプラスあなたは他の人が言及したことをintに変換する必要がある

1

入力コマンドはstrタイプを返します。

ifステートメントと比較する前に、user_input変数をintタイプに変換する必要があります。ここで

は例です:if int(user_input) == 1:

代替の方法は、このような冒頭でintに変数を変換することです:

user_input = int(user_input)

0

あなたはint型を使用して整数にあなたの変数を回すことができます( )コンストラクタです。変数を整数と比較します。ここで

try: 
    #if input isn't an integer, an exception will be thrown 
    user_input = int(input('> ')) 
except ValueError: #here we catch that exception. 
    print('Not an integer.') 

if user_input == 1: 
    print('execute this 1') 
elif user_input == 2: 
    print('execute this 2') 

は、例外処理の詳細については:https://docs.python.org/3.5/tutorial/errors.html#handling-exceptions

別のオプションは、文字列型にあなたの変数を比較することです。 input()関数のデフォルト出力は文字列です。例:

user_input = input('> ') 
if user_input == '1': 
    print('execute this 1') 
elif user_input == '2': 
    print('execute this 2') 

詳細組み込み関数の入力()の情報:https://docs.python.org/3.5/library/functions.html#input

0

問題がUSER_INPUT int型に変換する必要ことです。

user_input = input("Type 3 for money") 

または

代わりにあなたが 型の値をstrをする USER_INPUT を比較することができるユーザーの種類の文字の代わりに数字場合とValueErrorを避けるためにも変換しました。あなたのpython 3を使用している場合は

if user_input == '1': 
0

その後、入力はちょうど生の入力(文字列)を取得しますので、私は追加することをお勧め:eval(input("foo"))を、その入力の整数を行いますか、あなただけの、例えば文字列にそれらを比較することができましたif (user_input) == "1":または最後に、int(user_input)

を追加して、user_input変数を整数に変換できます
関連する問題