python - 为什么我的函数中出现错误?
问题描述
出于某种原因,计算的输出会multiply = func1_input * 9
从输入中给出一个重复的数字,例如5555555
,如果5
输入了。对于sum of digits
输出的计算也是您期望multiply
给出的。
def function1():
while True:
try:
func1_input = input("Please enter a number between 1 and 9: ")
val = int(func1_input)
if 1 <= val <= 9:
print("You selected: ",func1_input)
print("\n")
function2(func1_input)
break
else:
print("Invalid input. Please enter again.")
function1()
except ValueError:
print("number not entered")
def function2(func1_input):
func2_input = int(input("What is 10-1?: "))
if func2_input == 9:
print("Your number is: ",func2_input)
print("\n")
multiply = func1_input*9
print("Your number multiplied by 9 is: ",multiply)
function3(multiply)
else:
print("Invalid input. Please enter again.")
function2(func2_input)
def function3(multiply):
sum_of_digits = sum(int(digit) for digit in str(multiply))
print("Adding up the individual digits of your number gives: ",sum_of_digits)
function4(sum_of_digits)
解决方案
您的错误在行中function2(func1_input)
。应该是function2(val)
。
推荐阅读
- react-native - React Native - 模块 AppRegistry 不是注册的可调用模块
- javascript - 这段有趣的 JavaScript 代码是如何工作的?
- apache - 如何使用空查询参数进行 htaccess 重定向?
- google-cloud-platform - 为什么 Google Cloud Spanner 中节点越多更新延迟越高
- javascript - 反应从另一个文件夹导入 svg 文件
- python - Spotify Python API 调用超时问题
- c++ - 打印用户选择的行
- javascript - React JS:为什么我在 componentdidMount() 处出现错误?
- angular - Angular:如何将服务中的数据绑定到组件,以便服务中的新数据更新组件?
- gremlin - 如何获取 JanusGraph Gremlin 返回的子图的邻接矩阵?