如何将2个变量放在一起

时间:2017-10-21 16:21:47

标签: python

在python上我的当前代码是:

Import random 
Import sys
Import os
Print ("Hello! What is your name traveller?")
    name = sys.stdin.readline ()
    training = ",we are in peril! Start your training 
    now!"
first_mission = (name + training)
    Print (first_mission)

然后运行如下:(示例名称:Dave)

Dave  
,We are under peril! Start your training now!

如何让它像这样运行:

Dave , We are under peril! Start your training now!

谢谢!

2 个答案:

答案 0 :(得分:0)

除非某处出现错字/错误格式,否则只需在逗号后添加一个空格?

training = ", we are in peril! Start your training 

答案 1 :(得分:0)

我认为这样做会更容易:

import random 
import sys
import os

print("Hello! What is your name traveller?")

name = input()

training = " ,we are in peril! Start your training 
now!"

first_mission = (name + training)
print(first_mission)

为什么?

这会给你带来很多麻烦。当您调用sys.stdin.readline()函数时,将读取文本并在其末尾添加换行符(\ n)。这就是为什么你的文字在不同的行。