我目前无法在#34中完成此挑战;自动化无聊的事情":
我的代码是:
def collatz(number):
global seqNum
if (seqNum % 2 == 0):
return seqNum // 2
elif (seqNum % 2 == 1):
return 3 * seqNum + 1
print('What number would you like to use?')
seqNum = input()
number = int(seqNum)
i = number
while i > 1:
collatz(seqNum)
print(number)
我收到了这个错误:
"Traceback (most recent call last):
File "C:/Users/Administrative/AppData/Local/Programs/Python/Python36-32/collatzSeq.py", line 15, in <module>
collatz(seqNum)
File "C:/Users/Administrative/AppData/Local/Programs/Python/Python36-32/collatzSeq.py", line 3, in collatz
if (seqNum % 2 == 0):
TypeError: not all arguments converted during string formatting"
我知道我在编写代码时遇到错误,但我不明白它究竟是什么。非常感谢任何和所有的帮助!
我也在使用python 3。
答案 0 :(得分:4)
您正在对字符串进行算术运算,而不是整数。
不需要global
变量。将参数传递给函数,并让它相应地返回一个值。
def collatz(number):
if (number % 2 == 0):
return number // 2
elif (number % 2 == 1):
return 3 * number + 1
print('What number would you like to use?')
i = int(input())
while i > 1:
i = collatz(i)
print(i)
答案 1 :(得分:3)
这里有几个问题,但导致您出现异常的问题是您在函数中使用seqNum
,这是input()
返回的内容。 input()
返回一个字符串(至少在Python 3上)。而对于strings the %
,&#34;格式化运算符&#34;这也解释了异常消息,其中涉及&#34;字符串格式化&#34;。
您可以按如下方式编写(使用number
代替seqNum
):
def collatz(number):
# you pass the number to the function and you return, so no need for global
if number % 2 == 0: # python doesn't need parenthesis for "if"s
return number // 2
else: # it can only be even OR odd so no need to calculate the modulo again
return 3 * number + 1
# You can put the question as argument for "input" instead of printing it
seqNum = input('What number would you like to use?')
number = int(seqNum)
while number > 1 :
number = collatz(number) # assign the result of the function to "number"
print(number)
答案 2 :(得分:1)
seqNum
是一个字符串。
>>> "3" % 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting
>>>
答案 3 :(得分:1)
# This could be the simplest solution
def collatz(number):
while True:
if number <= 1:
break
elif number % 2 == 0:
number = number // 2
print(number)
elif number % 2 == 1:
number = 3 * number + 1
print (number)
try:
print('Enter a number \n')
number = int(input())
collatz(number)
except ValueError:
print('Invalid value, Enter a number.')
答案 4 :(得分:1)
对于像我这样的初学者,我认为这是最简单的理解:
def collatz(number):
while number != 1:
if number % 2 == 0:
number = number // 2
print(number)
else:
number = 3 * number + 1
print(number)
try:
collatz(number=int(input("Enter the number: \n")))
except ValueError:
print('Error: Invalid argument \nPlease enter a number')
答案 5 :(得分:0)
您似乎应该将i
传递给您的函数而不是seqNum
。
在您的函数中删除对seqNum
的所有引用,然后使用number
。
答案 6 :(得分:0)
嗨,我是新手编码,也是看这个练习。如果它在这里有用的是我采用1 x函数+ 2 x while循环的方法。我还注意到程序没有很好地处理零值作为输入:
# This program runs the Collatz sequence - Automate book, Chapter 3 practice project
# It includes the 'Input Validaton' additional exercise
# It also inlcudes a further test for zero value input as this makes collatz non-terminating
def collatz(number):
#test even
if number % 2 == 0:
return number // 2
#or implicit it is odd
else:
return 3 * number + 1
# Get the user input and validate - loop continues until non-zero integer entered
while True:
try:
print('Enter a non-zero number')
number = int(input())
if number == 0:
continue
else:
break
except ValueError:
print('Error: You must enter and integer')
# Iterate over the input number until it == 1
while number != 1:
# return value assigned to global var
number = collatz(number)
# output the result of collatz to screen
print(number)