Python 使用带有变量的 raw_input

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4356516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:24:29  来源:igfitidea点击:

Python use raw_input with a variable

python

提问by Favolas

Is it possible to use raw_input with a variable?

是否可以将 raw_input 与变量一起使用?

For example.

例如。

max = 100
value = raw_input('Please enter a value between 10 and' max 'for percentage')

Thanks,

谢谢,

Favolas

法沃拉斯

采纳答案by D.C.

You can pass anything that evaluates to a string as a parameter:

您可以将任何计算为字符串的内容作为参数传递:

value = raw_input('Please enter a value between 10 and' + str(max) + 'for percentage')

use + to concatenate string objects. You also need to explicitely turn non-strings into string to concatenate them using the str() function.

使用 + 连接字符串对象。您还需要明确地将非字符串转换为字符串以使用 str() 函数连接它们。

回答by Derrick

i think this might work

我认为这可能有效

value = input('Please enter a value between 10 and {} for percentage'.format(max))

回答by NickCortes

one more way to do it :)

另一种方法:)

value = raw_input('Please enter a value between 10 and %i for percentage' % (max))