2017-12-02 21 views
0

내 코드의 변수에 문제가 있습니다. 여기에 내 코드의 일부는 다음과 같습니다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") 

때마다 그것으로, 내가, 내가 쉘에서 실행 코드의 이 라인이 보여 "그는 자고"else 문을 얻을 "시"와 숫자로 I 입력 TOTAL_TIME 내 문제는 :

>>> 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은 여전히 ​​문자열이며 python 2에서 이상한/예기치 않은 결과 (정수/예상치 못한 결과)가 있습니다 (python 3에서는 적어도 오류를 지적하는 예외 (unorderable types: str() < int())가 발생합니다)

수정 :

당신은 부동 소수점 수의 문자열을 변환 할 float(timel)를 사용하려면
timel = float(non_decimal.sub('', total_time)) 
+0

을 변환 timel = float(timel)을해야한다 또는', 마지막 줄 '수 timel = 플로트 (timel) – zvone

0

.

float(a)a을 float로 변환하지만 원래 매개 변수에는 영향을주지 않습니다.

따라서, 라인은 실제로 timel