简体   繁体   English

从标准输入读取浮点值并在time.sleep中使用

[英]Reading a float value from standard input and using in time.sleep

I'm currently struggling with simple code, it works with seconds but I want to allow users to use minutes instead, which is much easier. 我目前正在使用简单的代码,它需要几秒钟的时间,但是我想允许用户使用分钟,这更加容易。 Here it is: 这里是:

import time
import os
import math
import subprocess

input1 = raw_input("Broj minuta:")
min = input1 * 60
min1 = float(min)
print min1
time.sleep(min1)
os.system("shutdown")

I get this error: 我收到此错误:

Broj minuta:2
Traceback (most recent call last):
  File "timer.py", line 8, in <module>
    time.sleep(min)
TypeError: a float is required

When I try to convert it to float using code below, it says that sleep time is big, and it is, if I choose 2 minutes I get: 当我尝试使用下面的代码将其转换为float时,它说睡眠时间很大,如果我选择2分钟,我会得到:

min = input1 * 60
min1 = float(min)

Broj minuta:2
2.22222222222e+59
Traceback (most recent call last):
File "timer.py", line 10, in <module>
   time.sleep(min1)
OverflowError: sleep length is too large
input1 = raw_input("Broj minuta:")

raw_input returns a string. raw_input返回一个字符串。 You need to convert that to a number, like this 您需要像这样将其转换为数字

input1 = int(raw_input("Broj minuta:"))

If you don't do this, let say if you enter 2 , it will still be a string and you are doing 如果您不这样做,请说如果输入2 ,它仍然是一个字符串并且您正在执行

input * 60

which means '2' * 60 , which is equal to '222222222222222222222222222222222222222222222222222222222222' and it is still a string. 这表示'2' * 60 ,等于'222222222222222222222222222222222222222222222222222222222222'并且它仍然是字符串。 That's why time.sleep(min) complains that 这就是为什么time.sleep(min)抱怨

TypeError: a float is required

In the second case, you are converting '222222222222222222222222222222222222222222222222222222222222' to a float properly, but the value is 2.22222222222e+59 , which is tooo big for time.sleep . 在第二种情况下,你将'222222222222222222222222222222222222222222222222222222222222'为float正常,但价值2.22222222222e+59 ,这是tooo大的time.sleep

(Your traceback must be out of date; it shows time.sleep(min) (in which case the error is justified), but your code has time.sleep(min1) .) (您的回溯必须已过时;它显示time.sleep(min) (在这种情况下,错误是有道理的),但是您的代码具有time.sleep(min1) 。)

The issue is that the result of raw_input is a string. 问题是raw_input的结果是一个字符串。 When you write input1 * 60 , you repeat the string 60 times (ie instead of 120, you get '222222222222222222222222222222222222222222222222222222' ). 当您编写input1 * 60 ,您将字符串重复60次(即,不是120,而是得到'222222222222222222222222222222222222222222222222222222' )。

You're performing the conversion too late. 您执行转换太晚了。 input1 is a string, and multiplying a string by an integer input1是一个字符串,并将字符串乘以整数

min = input1 * 60

does string repetition: '12' * 60 == '12121212... 是否重复字符串: '12' * 60 == '12121212...

Instead, convert to float, then multiply: 相反,转换为浮点数,然后相乘:

min = float(input1) * 60

The variable returned by raw_input is a string. raw_input返回的变量是一个字符串。 To add more information to what is happening in your code, here is an example session. 要向代码中发生的事情添加更多信息,下面是一个示例会话。

In [1]: i = raw_input("Something")
Something20

In [2]: i
Out[2]: '20'

In [3]: type(i)
Out[3]: str

In [4]: i*60
Out[4]:     '202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020'

In [5]: float(i*20)
Out[5]: 2.02020202020202e+39

Out [2] shows the string '20' . Out [2]显示字符串'20' This is confirmed when we check the type of i , which is str . 当我们检查i的类型str时,可以确认这一点。 Now, in Python, when you multiply a string with a number x , you get a string with x times the original string repeated. 现在,在Python中,当您将一个字符串与数字x相乘时,您会得到一个x乘以原始字符串重复次数的字符串。

This is what was happening in your code. 这就是您的代码中正在发生的事情。 Thus, as suggested in the other answer, you need to cast your input to a float. 因此,如另一个答案中所建议,您需要将输入强制转换为浮点数。

Another, way of doing this (on Python 2.x) is to NOT use raw_input() and instead use input() 另一种方法(在Python 2.x上)是不使用raw_input()而是使用input()

In [6]: j = input('Something else?')
Something else?20

In [7]: j
Out[7]: 20

In [8]: j*60
Out[8]: 1200

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM