私は、情報科学のためのPythonの練習用に以下のようなPythonコードを作成しました。コードは実行されますが、数値が1より大きいユーザーの入力フォームは認識されません。いかなる援助も感謝します。Python 3 - コードは1より大きいユーザー入力を数値として認識しません
def isfloat(string):
try:
float(string)
if float(string) == True:
return True
except ValueError:
return False
user_input = input('Please enter a real number. Type \"done\" to exit and tally your entries \n> ')
data = 0
count = 0
while isfloat(user_input) == True:
data = data + float(user_input)
count = count + 1
user_input = input("Please enter another value \n> ")
isfloat(user_input)
else:
if (isfloat(user_input) == False) and (user_input == "done"):
print("The sum of your entries is: " + str(data))
print("The number of entries was: " + str(count))
exit()
else:
print("The entry was not a numeric value \n")
print("The sum of your valid entries is: " + str(data))
print("The number of valid entries was: " + str(count))
exit()
'float型(文字列)STRING'はは' true'ので、 ' "1" または' ' "1.0"' 'である場合== true'には通過します値 '1'を持つ 'int'サブクラスです。しかし、それはひどいテストです。 – ShadowRanger
私は同意しますが、残りのコードを変更するつもりはありませんでした。私は、OPが尋ねた問題を解決するつもりでした。私はそれをより明確にするために自分の投稿を変更します。 – caleb531