0

私は初心者のPythonプログラマーです。助けが必要です。

lightcolor=int(input("Enter Red,Green,Yellow,White,Purple,Blue,Orange,Brown,or Black->"))
if lightcolor=="Red":
    print("Red Light-Please stop!!")
elif lightcolor=="Green":
    print("Green Light-Please continue")
elif lightcolor=="Yellow":
    print("Yellow Light-speed up")
elif lightcolor=="White":
    print("White Light-its too bright")
elif lightcolor=="Purple":
    print("Purple Light-pretty")
elif lightcolor=="Blue":
    print("Blue Light-thats unusual")
elif lightcolor=="Orange":
    print("Orange Light-bright as the sun")

elif lightcolor=="Brown":
    print("Brown Light-like dirt")
elif lightcolor=="Black":
    print("Black Light-very dark")
else:
    print("Sorry no such color"),lightcolor

色を入力するたびに基本エラーで int() の無効なリテラルを取得するのはなぜですか? python 3を使用しています。タンクを助けてください intを修正しましたが、うまくいきました。

4

2 に答える 2

1

int入力を整数に変換しようとするため、int('Red')がスローされValueErrorます。

int 呼び出しのraw_input代わりに使用して削除する必要があります。input

lightcolor=raw_input("Enter Red,Green,Yellow,White,Purple,Blue,Orange,Brown,or Black->")
于 2013-10-02T02:58:38.210 に答える
1

を削除しintて にするdictことが、今後の方法です...

colours = {
    # List colours... and spelling variations... (put in lower case for easier comp.)
    'red': 'Stop',
    'green': 'Go',
    'black': 'Dark'
}
# Get colour and prompt based on the colours in the dictionary
input_colour = input('Enter a colour (one of: {})'.format('|'.join(colours))
# Try and get description from colours, otherwise use "Not Valid!"
print('That color is:', colours.get(input_colour.lower(), 'Not Valid!'))
于 2013-10-03T02:03:46.520 に答える