は私のコードです:なぜinput()は常に文字列を返しますか?ここ
age = input("How old are you?: ")
print (age)
print (type(age))
結果:
How old are you?: 35
35
class 'str' <<--- This is problem!
しかし、私が使用している場合..
age = int(input("How old are you?: "))
print (age)
print (type(age))
とした場合、 "アレックス"
How old are you?: alex
Traceback (most recent call last):
File "/Users/kanapatgreenigorn/Desktop/test.py", line 1, in age = int(input("How old are you?: ")) ValueError: invalid literal for int() with base 10: 'alex'
検証の出番です。あなたが年齢を求めている場合は、* *番号を期待しています。入力をキャストしたり、try/expectブロックに投げたりしないでください。タイプが期待どおりでない場合はエラーを返します。 –
それは問題ではなく有用なことではありませんか? 「あなたは何歳ですか?」という質問に対する「アレックス」の有効な答えを考えてみたいですか? – trincot
チェックアウトしたい場合がありますhttp://stackoverflow.com/questions/354038/how-do-i-check-if-a-string-is-a-number-float-in-python – ToothlessRebel