2017-12-02 8 views
0

私のコードで変数に問題があります。私はそれで「午後」と番号などの入力TOTAL_TIMEが、私はシェルで実行されていたこれらのコード行を示して 「彼は眠っている」、else文を取得するたびにPython - 浮動小数点変数は常に比較として最大値を返します

import re 
name = raw_input("Input the Character's name: ") 
name = name.lower() 

season = raw_input("Input the season: ") 

rainyday = raw_input("Is it a rainy day? ") 
total_time = raw_input("What time is it?(Input as e.g 1.30 am") 
non_decimal = re.compile(r'[^\d.]+') 
timel = non_decimal.sub('', total_time) 
float(timel) 
print timel 
#ALEX 
if name == "alex": 
    if rainyday == "yes": 
     if "am" in total_time: 
      if time > 8.00: 
       print("He's in the entryway of his house until 1:00 PM") 
      else: 
       print("He's in his room until 8:00 AM") 
       print("!") 
     elif timel < 1: 
      print("He's in the entryway of his room until 1:00 PM") 
      print("y") 
     elif timel < 4.2: 
      print("He's leaving his room to go to the dog pen until 6:30 PM") 
     elif timel < 6.3: 
      print("He's at the dog pen until 6:30") 
     elif timel < 8.0: 
      print("He's in the entryway of his house until 8:00 P.M") 
     elif timel < 10.0: 
      print("He's standing by the dresser in his room") 
     else: 
      print("He's sleeping") 

:ここに私のコードの一部であります私の問題:

>>> print timel 
2 
>>> timel < 3 
False 
>>> 2 < 3 
True 
>>> timel < 1000000000 
False 

timelは、それが定義されているにもかかわらず、常に他のすべてよりも大きくなっている未定義の数であるようです。私はこの問題の助けに大いに感謝します。

答えて

0
timel = non_decimal.sub('', total_time) 
float(timel) 

最後の行は、timelをfloatに変換しません。 floatはインプレースで動作しません。そのtimel

はまだ文字列であり、整数でエラーなししかし不思議/予期しない結果と比較/(パイソン3には、少なくともエラーを指摘例外(unorderable types: str() < int())で失敗)パイソン2に浮い

修正:

timel = float(non_decimal.sub('', total_time)) 
+0

または変換するtimel = float(timel)なければなりません – zvone

0

あなたは浮動小数点数に文字列を変換するためにfloat(timel)を使用したいです。

float(a)aはfloatに変換されますが、元のパラメータには反映されません。

したがって、ラインは、最後の行は、 `` timel =フロート(timel)とすることができる、実際timel

関連する問題