3

我想让它让用户输入他的姓名/年龄,然后输出十年后的姓名和年龄。我已经这样设置了:

    print("Let's find out how old you will be in 10 Years.\n")
    name = input("name: ")

    print("\nNow enter your age,",name,"\n")
    age = int(input("age: "))

    ageinten = age + 10

    print("\n",name,"you will be",ageinten,"in ten years.")

    input("Press Enter to close")

输出是这样的:

    Let's find out how old you will be in 10 Years.

    name: Example

    Now enter your age, Example 

    age: 20

     Example you will be 30 in ten years.
    Press Enter to close

但我希望它没有示例之前的空格:

    Example you will be 30 in ten years.
    Press Enter to close

任何人都可以帮助解决这个问题吗?

4

2 回答 2

5

更好地使用字符串格式

print('\n{} you will be {} in ten years.'.format(name, ageinten))

或使用sep='',但您必须在字符串中添加尾随和前导空格。:

print("\n", name, " you will be ", ageinten, " in ten years.", sep='')

的默认值sep是一个空格,这就是你得到一个空格的原因。

演示:

>>> name = 'Example'
>>> ageinten =  '20'
>>> print("\n",name," you will be ",ageinten," in ten years.", sep='')

Example you will be 20 in ten years.
>>> print('\n{} you will be {} in ten years.'.format(name, ageinten))

Example you will be 20 in ten years.
于 2013-11-08T10:42:05.387 回答
0

尝试使用只是print()给出换行符。这似乎最适合您当前的风格:

print("Let's find out how old you will be in 10 Years.\n")
name = input("name: ")

print()
print("Now enter your age,",name)
print()
age = int(input("age: "))

ageinten = age + 10

print()
print(name,"you will be",ageinten,"in ten years.")

input("Press Enter to close")
于 2013-11-08T11:04:45.733 回答