TypeError: not all arguments converted during string formatting 2.0
TypeError: not all arguments converted during string formatting 2.0
我正在尝试回调我创建的拆分列表,但出现此错误:
TypeError: not all arguments converted during string formatting
这是我的代码:
user_string = (input('Enter input string: \n'))
if ',' not in user_string:
print ('Error: No comma in string.\nEnter input string: ')
if ',' not in user_string:
print ('Error: No comma in string.\nEnter input string: ')
if ',' not in user_string:
print ('Error: No comma in string.\nEnter input string: ')
else:
print (user_string)
new_tokens = user_string.split (',')
print('First word: ' %s (new_tokens[0]))
print('Second word: ' %s (new_tokens[1]))
代码有几处错误。
首先,字符串格式:
print('First word: ' %s (new_tokens[0]))
print('Second word: ' %s (new_tokens[1]))
Python 有来自 C 的 many ways to format strings. You seem to be trying to use the printf
-style formatting,但您放错了 %s
字符。应该是 format % values
:
print('First word: %s' % (new_tokens[0]))
print('Second word: %s' % (new_tokens[1]))
请注意,虽然 %
风格没有问题,但如果您已经使用 Python3,最好使用 .format()
语法或更新的 f -strings 语法在 Python3.6.
中引入
第二个,输入提示:
user_string = (input('Enter input string: \n'))
if ',' not in user_string:
print ('Error: No comma in string.\nEnter input string: ')
我的理解是,您是在提示用户输入一个字符串,但它需要有一个逗号。如果输入没有逗号,则再次提示用户。但是你的代码的问题是你没有更新user_string
。它不会神奇地或自动更新,您需要再次将 input
值重新分配给 user_string
。
如果您有用户可以进行的预定义尝试次数(根据您的示例看起来是 3 次),您可以制作一个提示循环,直到进行最大尝试或直到用户提供正确的输入。它看起来像这样:
user_string = ""
max_attempts = 3
current_attempt = 0
while (current_attempt < max_attempts):
# Get user input
# Python2
user_string = raw_input('Enter input string: \n')
# Python3
#user_string = input('Enter input string: \n')
# Check if user input is correct
if ',' not in user_string:
print('Error: No comma in string.')
current_attempt += 1
else:
# Stop the loop
break
print(user_string)
if current_attempt == max_attempts:
print('Error: I told you to input string with comma')
else:
# rest of the code
我正在尝试回调我创建的拆分列表,但出现此错误:
TypeError: not all arguments converted during string formatting
这是我的代码:
user_string = (input('Enter input string: \n'))
if ',' not in user_string:
print ('Error: No comma in string.\nEnter input string: ')
if ',' not in user_string:
print ('Error: No comma in string.\nEnter input string: ')
if ',' not in user_string:
print ('Error: No comma in string.\nEnter input string: ')
else:
print (user_string)
new_tokens = user_string.split (',')
print('First word: ' %s (new_tokens[0]))
print('Second word: ' %s (new_tokens[1]))
代码有几处错误。
首先,字符串格式:
print('First word: ' %s (new_tokens[0]))
print('Second word: ' %s (new_tokens[1]))
Python 有来自 C 的 many ways to format strings. You seem to be trying to use the printf
-style formatting,但您放错了 %s
字符。应该是 format % values
:
print('First word: %s' % (new_tokens[0]))
print('Second word: %s' % (new_tokens[1]))
请注意,虽然 %
风格没有问题,但如果您已经使用 Python3,最好使用 .format()
语法或更新的 f -strings 语法在 Python3.6.
第二个,输入提示:
user_string = (input('Enter input string: \n'))
if ',' not in user_string:
print ('Error: No comma in string.\nEnter input string: ')
我的理解是,您是在提示用户输入一个字符串,但它需要有一个逗号。如果输入没有逗号,则再次提示用户。但是你的代码的问题是你没有更新user_string
。它不会神奇地或自动更新,您需要再次将 input
值重新分配给 user_string
。
如果您有用户可以进行的预定义尝试次数(根据您的示例看起来是 3 次),您可以制作一个提示循环,直到进行最大尝试或直到用户提供正确的输入。它看起来像这样:
user_string = ""
max_attempts = 3
current_attempt = 0
while (current_attempt < max_attempts):
# Get user input
# Python2
user_string = raw_input('Enter input string: \n')
# Python3
#user_string = input('Enter input string: \n')
# Check if user input is correct
if ',' not in user_string:
print('Error: No comma in string.')
current_attempt += 1
else:
# Stop the loop
break
print(user_string)
if current_attempt == max_attempts:
print('Error: I told you to input string with comma')
else:
# rest of the code