2017-04-19 1 views
1

ich eine Variable in einer print-Anweisung setzen, und ich bekomme diese Fehlermeldung:Python Variable in String Fehler

Traceback (most recent call last): 
    File "C:\Users\Samuel\workspace\First Python Project\com\fireboxtraining\Person.py", line 13, in <module> 
    print("Dude, %s is not yes or no... come on.") % (game) 
TypeError: unsupported operand type(s) for %: 'NoneType' and 'str' 

Dies ist mein Code:

''' 
Created on Apr 17, 2017 

@author: Samuel 
''' 
game = input("Would you like to play a game? Please answer with \"Yes\" or \"No\".") 
game = game.lower() 
if game == "yes": 
    print("Great! Let's play.") 
elif game == "no": 
    print("Ok, I guess we won't play.") 
else: 
    print("Dude, %s is not yes or no... come on.") %(game) 

Ich bin nur das Lernen Python und ich verstehe nicht, warum das nicht funktioniert. Bitte helfen Sie!

+0

Sie müssen den Fall behandeln, wenn Sie eine Antwort nicht eingegeben zu tun. Der Wert ist leer oder Kein, daher ist es nicht sinnvoll, ihn auszudrucken. – anonyXmous

+1

die letzte Zeile sollte 'print sein (" Dude,% s ist nicht ja oder nein ... komm schon. "% (Spiel))'. Achten Sie auf die rechte Klammer. – pkuphy

Antwort

1

Es ist eine einfache Klammer aus. Sie haben:

print("Dude, %s is not yes or no... come on.") %(game) 

es sein sollte:

print("Dude, %s is not yes or no... come on." % (game))