Python将raw_input与变量一起使用

时间:2022-08-29 20:15:16

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

4 个解决方案

#1


9  

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()函数将它们连接起来。

#2


2  

Python Language Reference, §5.6.2, "String Formatting Operations"

Python语言参考,§5.6.2,“字符串格式化操作”

#3


1  

i think this might work

我认为这可能会奏效

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

#4


1  

one more way to do it :)

还有一种方法:)

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

#1


9  

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()函数将它们连接起来。

#2


2  

Python Language Reference, §5.6.2, "String Formatting Operations"

Python语言参考,§5.6.2,“字符串格式化操作”

#3


1  

i think this might work

我认为这可能会奏效

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

#4


1  

one more way to do it :)

还有一种方法:)

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