OverflowError:重复的字符串太长

时间:2014-12-14 13:02:04

标签: python

当我使用收音机时,我正在写一个python脚本。当试图计算波长时,我得到一个OverflowError。有可能克服这个问题吗?

这是我的代码:

while 1:
cmd = input("Enter command: ")
if cmd == 'wavelength':
    freq = input("Enter frequency (mHz): ") * 10**6 
    wl = freq * 299792458
    print (wl)

这是输出:

Enter command: wavelength
Enter frequency (mHz): 33
Traceback (most recent call last):
  File "C:/Users/Ross/Desktop/Programming/Python/Radio/main.py", line 5, in <module>
    wl = freq * 299792458
OverflowError: repeated string is too long        

3 个答案:

答案 0 :(得分:3)

如果你正在使用Python 3.x,input会返回一个字符串对象。

如果要对乘法进行编号,请将字符串的返回值转换为数值。

freq = int(input("Enter frequency (mHz): ")) * 10**6 

freq = float(input("Enter frequency (mHz): ")) * 10**6 

答案 1 :(得分:1)

您正在乘以字符串:

input("Enter frequency (mHz): ") * 10**6
wl = freq * 299792458

你可能意味着:

freq = int(input("Enter frequency (mHz): ")) * 10**6
wl = freq * 299792458

答案 2 :(得分:1)

您的代码

if cmd == 'wavelength':
    freq = input("Enter frequency (mHz): ") * 10**6 
    wl = freq * 299792458
    print (wl)

不只是一个问题,它有两个问题

  1. input函数返回一个字符串,你连接自己一百万次,比如“107.6107.6107.6 ...... 107.6”,只有一百万次。
    您希望将响应转换为浮点数nuber,使用float之前将其乘以10 ** 6

    freq = float(input("Enter frequency (mHz): ")) * 10**6
    
  2. 波长计算错误 示例尺寸分析:[L] != (1/[T]) * [L]/[T] = [L]/[T]^2

    wl = c/freq
    
相关问题