输入变量可以通过逗号分隔吗?
编写一个程序,该程序从用户读取数量,然后是几英寸。读取这些值后,您的程序应计算并显示等效数厘米
我编写以下代码的等效厘米数:
feet = int(input('Enter feet: '))
inches = int(input('Enter inches: '))
centimeters = ((feet * 12) + inches) * 2.54
print("Height in centimeters: ", centimeters)
我可以在同一条线上均和英寸输入用户输入吗?例如,提示可能看起来像这样:
Enter height ____ , ____
两个输入都会被逗号隔开,以表明左脚是脚,右为英寸。这是否可能,并且两个数字都可以作为不同的变量存储,以完成转换为厘米?
Write a program that reads a number of feet from the user, followed by a number of inches. Once these values are read, your program should compute and display the equivalent number of centimeters
I wrote the following code:
feet = int(input('Enter feet: '))
inches = int(input('Enter inches: '))
centimeters = ((feet * 12) + inches) * 2.54
print("Height in centimeters: ", centimeters)
Can I have the user input both feet and inches on the same line? For example the prompt might look something like this:
Enter height ____ , ____
Both inputs would be separated by a comma to indicate that the left is feet and the right is inches. Is this possible and would both numbers be able to be stored as different variables as to complete the conversion into centimeters?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将
input()
函数与split()
方法一起使用。input()
函数将用户输入返回为String
对象。因此,您可以使用split()
方法将用户输入(字符串)分开。示例:上面的代码无问题,而用户输入没有空格,示例
10,12
。使用,
作为隔板的方法,请小心10,12
等输入案例。在这种情况下,在12
之前有空间。因此,在获得上面显示的用户输入后,您可以使用strip()
方法在字符串的开头或结尾处摆脱空格。或,
您可以要求用户提供按空间分开的输入。
You can use
input()
function along withsplit()
method.input()
function returns the user input as astring
object. So, you can usesplit()
method to split the user input (string) whatever way you want. Example:Above code works with no issues, while the user input have no spaces, example
10,12
. With the approach of using,
as separator be careful of the input cases like10, 12
. In this case, there is space before12
. So, after getting user input as shown above, you can usestrip()
method to get rid of spaces at the beginning or end of a string.OR,
you can ask user to provide input separated by space.
我在这里有点新,但是如果我没记错的话,您可能应该使用一个拆分功能(((您想拆分的东西)。 然后,
可以索要输入。
您 9)
。从列表中,
您也可以单独输入转换的输入表,但据我所知,包括输入中的拆分是最简单的方法。
I'm a little new here, but if I remember correctly, you should probably use a split function, ((something that you want to split.split()). So firstly, you could assign feet and inches to 0 as a variable.
Then, you can ask for the input.
At this point you should separate your input, for example, '6, 3'. Python will now assign feet to a new variable(feet = 5), and inches as well (inches = 9). You can now convert this into centimeters, and both numbers will be stored as different variables. Alternatively, if there are multiple inputs you will be having, be sure to include first have:
This is so that you can now independently retrieve certain information from the list, perhaps to make a table of converted inputs?
You could also input separately, but as far as I know, including the split in the input is the easiest way to go. Hope this helps :D