python - 使用 print 语句在 Python 中显示变量值?

标签 python

我创建了一个 Python 程序,它可以猜测程序员心中所想的数字。一切都是工作文件,但我不知道如何在打印语句中以打印语句显示数字的方式使用猜测。我尝试添加“猜测”一词,但它不起作用。我是 C 程序员,第一次使用 Python,所以我无法弄清楚。

hi = 100
lo = 0
guessed = False 

print ("Please think of a number between 0 and 100!")

while not guessed:
    guess = (hi + lo)/2
print ("Is your secret number " + //Here i need to Display the guessed Number + "?")
user_inp = raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too"
                     "low. Enter 'c' to indicate I guessed correctly: ")

if user_inp == 'c':
    guessed = True
elif user_inp == 'h':
    hi = guess
elif user_inp == 'l':
    lo = guess
else:
    print ("Sorry, I did not understand your input.")

print ("Game over. Your secret number was: " + //Here i need to display final number saved in guess.)

最佳答案

只需将其转换为字符串即可。

print ("Game over. Your secret number was: " + str(guess))

您还可以使用字符串格式。

print("Game over. Your secret number was {}".format(guess))

或者,由于您有 C 语言背景,因此采用旧式字符串格式。

print("Game over. Your secret number was %d." % guess)

关于python - 使用 print 语句在 Python 中显示变量值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22742367/

相关文章:

python - 正则表达式忽略不需要的匹配引号

python - 为什么有些方法使用点符号而其他方法不使用?

python - Divio CMS 存储库的设置

python - 从数组 python 创建数据框

python - 无法将按钮添加到 pyplot?

python - 测试具有相同范围的变量

python - 不使用 WebDriverWait 我的代码返回 : element click intercepted/with WebDriverWait returns 'NoneType' object is not iterable

python - 如何在c或python中将数字的高8位移动7次?

python - 通过提供一些参数来重新定义一个函数

python - 如何在Python中从列表中获取整数并构造哈希表?