Я беру базовый класс по компьютерному программированию и наткнулся на проблему, в которой я не знаю, как использовать переменную из предыдущей функции. Подсказка состоит в том, чтобы взять начальное и конечное значения и вычислить числа между ними. Как я могу это изменить?
Я действительно ничего не пробовал. Я действительно застрял.
def getStartNum():
startValue = int(input("Enter starting number: "))
while(startValue<0):
print("Invalid input.")
startValue = int(input("Enter starting number: "))
return startValue
def getStopNum():
stop = int(input("Enter the ending number: "))
while(stop <= startValue):
print("Ending number must be greater than the starting value.")
stop = int(input("Enter the ending number: "))
return stop
def sumOfNums(startValue, stop):
total = 0
for i in range(startValue, stop+1, 1):
total+=i
return total
def productOfNums(startValue, stop):
product = 1
for j in range(startValue, stop+1, 1):
product*=i
return product
st = getStartNum()
sp = getStopNum()
ns = sumOfNums(st, sp)
p = productOfNums(st, sp)
print("The sum of the sequence is:", ns)
print("The product of the sequence is:", p)
cont = input("Do you want to continue? y/n: ")
Сообщение об ошибке:
while(stop <= startValue):
NameError: name 'startValue' is not defined
Я ожидал, что вывод распечатает сумму и продукты сразу