gpt4 book ai didi

python - 为什么变量 YourName = input ("What is your name?") 打印用户输入问题而不是用户输入

转载 作者:行者123 更新时间:2023-12-04 03:42:20 25 4
gpt4 key购买 nike

我创建了一个程序来显示一些预设变量,然后是接受用户输入的变量。我的印象是,在用户输入他们的名字后,以下 print() 函数将简单地使用用户的输入,而不是在变量输入旁边重复问题。

#Setup the required variables for Dialog1 and Dialog 2
MyName = "Shawn"
MyNumber = "seven"

#Sting varibles that will the print() function.
s = "Hello, my name is " + MyName + "."
ss = "My lucky number is " +MyNumber + "."

# Alternative method for both (s) and (ss) to be combined on one line.
## s = "Hello, my name is " + MyName + ". My lucky number is " +MyNumber + "."

#Print() function for the first dialog.
print(s)
print(ss)

#Dialog2 that includes user input
YourName = input("What is your name?")
print("Your name is " +YourName)
##YourNumber = input("What is your lucky number?")
##print("Your lucky number is " +YourNumber + ".")

Eric IDE(Linux Mint 20.x) 的输出是:

What is your name?Jason
Your name is What is your name?Jason

我想要的是 print() 的输出只是 Your name is Jason

最佳答案

您的代码运行良好。正如@Mick 评论的那样,您的 IDE 可能有问题。在这里,我添加了一些可能对您有用的简单修改。

MyName = "Shawn"
MyNumber = "seven"

print(f"Hello, my name is {MyName}.")
print(f"Hello, my name is {MyNumber}.")

YourName = input("What is your name?")
print(f"Your name is {YourName}")

为了说明这一点,我删除了您的评论。添加f'string goes here',您可以通过在其中放置变量来进行字符串格式化。

关于python - 为什么变量 YourName = input ("What is your name?") 打印用户输入问题而不是用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65767734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com