我的数字不会为我的算法加起来,我不知道为什么


print ("Perimeter for Total House Floor Remodeling Calculator")   
print(" ")   
width1 = input ("Please enter the width of the floor: ")   
length1 = input ("Please enter the length of the floor: ")   
print (" ")   
length = length1 * 2   
width = width1 * 2   
perimeter = (length + width)   
print ("The perimeter of the floor is: ",perimeter)    

一旦我输入我的电话号码,可以说我的宽度为5,而长度为5,我的周边将以5555而不是20。p>

input()函数为您提供字符串,您需要在进行任何计算之前将它们转换为整数。尝试:

width1 = int(input("Please enter the width of the floor: "))   
length1 = int(input("Please enter the length of the floor: ")) 

Python可以在字符串之间进行乘法操作,从而重复它们。IE:'5'*3给您'555',因为它是字符串。而5*3给出15,因为它是一个整数。

您是从Input()捕获一个字符串的数据。您需要将其投入一个数字。当您有字符串aka" 12"并运行" 12"*2时,它将输出" 1212"。

raw_output = input("Enter a number: ")
print("Type of raw_output: {0}".format(type(raw_output))
actual_integer = int(raw_output)
print("Type of actual_integer: {0}".format(type(actual_integer))

来自帮助功能

Help on built-in function input in module builtins:
input(...)
    input([prompt]) -> string
Read a string from standard input.  The trailing newline is stripped.
If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.
On Unix, GNU readline is used if enabled.  The prompt string, if given,
is printed without a trailing newline before reading.

由于不需要在Python中明确提及数据类型,因此您必须键入cast(强行转换为其他数据类型)您的输入才能将其视为整数。

width1 = int(input("Please enter the width of the floor: "))
length1 = int(input("Please enter the length of the floor: ")) 

这应该起作用!

我不确定您正在使用哪种版本的Python,但在我的计算机上有2.7.13上,您的代码工作正常。检查您有哪个版本,并让我们知道。

相关内容

最新更新