Python基本功能程序

时间:2015-09-29 19:06:57

标签: python

我是python的新手,我被要求制作一个带有函数的程序。

##Write a program that converts between centimetres, and inches and vice versa, by:
##asking the user to input a number
##asking the user to choose between converting from centimetres to inches or from inches to centimetres
##calculating and outputting the result using functions

number = int(input("Please enter a number"))
print ("Please write 'one' if you would like to convert from inches to centimeters"/n "please write 'two' if you would like to convert from centimeters to inches.")

def one():
             print("Your number is", number*2.54, "in centimetres")

def two():
             print ("Your number is", number/2.54, "in inches")

             return

当我的程序运行时,它只是说"语法错误"而且没有跑。

任何帮助将不胜感激 - 谢谢。

2 个答案:

答案 0 :(得分:5)

第一个问题

您在此行中缺少结束括号:

number = int(input("Please enter a number")

你应该有这个:

number = int(input("Please enter a number"))

第二个问题

你正试图转换"一个"到一个int。你不能这样做。您需要重新考虑您希望如何获取用户输入以确定要执行哪种方法的方法。你可以坚持使用"一个"和"两个"。但试着这样做:

number = input("Please enter a number")

从那里你应该使用一个简单的条件语句来调用你想要调用的方法。我会把这个留给你弄清楚。

第三个问题

您没有正确使用换行符。您无法按照自己的方式设置换行符。首先,换行符的正确用法是\n,必须在引号内。所以你的行应该是这样的:

print ("Please write 'one' if you would like to convert from inches to centimeters\nplease write 'two' if you would like to convert from centimeters to inches.")

答案 1 :(得分:0)

似乎正在处理这些变化。

number = int(input("Please enter a number")) 
print ("Please write 'one' if you would like to convert from inches to centimeters \n please write 'two' if you would like to convert from centimeters to inches.") 

def one(): 
    print("Your number is", number*2.54, "in centimetres") 

def two(): 
    print ("Your number is", number/2.54, "in inch")