首先,有几点:
“input”和“raw_input”的区别在于,input将返回一个数值,而raw_input将返回一个字符串值。如果您打算在数值运算中使用原始输入,则必须将其转换为int或float。同样,如果希望用户输入单词或短语,则必须使用raw_input。在
其次,打印功能会自动将输出放在新行上。如果要在输出中添加另一新行,请在字符串中使用“\n”。在
最后,将半径投射到一个浮子上,确保它可以打印成一个浮子。如果在将其转换为字符串之前不将其转换为float,python将为您进行格式化。在
下面我取下你的代码,注释掉有缺陷的部分,并将我的修复直接放在它们下面:print("Do you want to find the area of a circle? ")
# again = input("Enter 'y' for yes, or enter 'n' to exit the program: ")
again = raw_input("Enter 'y' for yes, or enter 'n' to exit the program: ")
while (again == 'y'):
pi = 3.14
# radius = raw_input(" Input the radius of the circle: ")
radius = input("Input the radius of the circle: ")
area = pi * radius * radius
# print("A circle with a radius of " + str(float(radius)) + " has an area of " + "{0:.2f}".format(area))
print("A circle with a radius of " + str(radius) + " has an area of " + "{0:.2f}".format(area))
# print()
# print("Would you like to run another? ")
print("Would you like to run another? ")
# again = input("Enter 'y' to run another, enter 'n' to exit: ")
again = raw_input("Enter 'y' to run another, enter 'n' to exit: ")
print("Have a nice day :) ")
希望这有帮助!在
在python 3中使用以下内容:
^{pr2}$