2017-10-21 127 views
-4

Python的我當前的代碼是:如何把2個變量一起

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! 

謝謝!

+0

是不是有一個原因,你不只是使用內置的輸入函數來獲取名稱? – Kurt

回答

0

除非在這裏有一個錯字/格式錯誤,只需在逗號後面加一個空格?

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

在我看來,OP在談論增加的換行符。 – Gnudiff

+0

是的,看起來像。在當時,沒有像現在這樣格式化,只有可見的區別是空間 – Zulfiqaar

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)。這就是爲什麼你的文字在不同的路線上。

+0

你也可以擺脫進口。 – Kurt

+1

@Kurt好點。儘管他們可能會在程序中進一步需要它。 –