In the script below, I would like to give, for example, the number 2. I have of course moved to the if where the message will show me. Then he added to the sum 2. Then I give again, for example, 3, etc. The sum will reach 8 and get the message END.
I do not know if int (a)
is necessary.
from sys import exit
class Glowna_klasa(object):
def start(self):
suma = 0
a = int(input("Take a number: "))
print("Now suma have: ", suma)
if int(suma) == 8:
print("END")
exit(1)
if int(a) == 2:
print("2 sztuki mają: 1.40")
# here need add to suma ?
return self.start()
elif int(a) == 3:
print("3 sztuki mają: 2.30")
return self.start()
elif int(a) == 4:
print("4 sztuki mają: 3.20")
return self.start()
else:
print("Error, need number")
crv = Glowna_klasa()
crv.start()
This is what i need:
>>>Take a number:
2
>>>2 sztuki mają: 1.40
>>>Now suma have: 2
>>>Take a number:
2
>>>2 sztuki mają: 1.40
>>>Now suma have: 4
>>>Take a number:
4
>>>4 sztuki mają: 3.20
>>>Now suma have: 8
>>>END
答案 0 :(得分:0)
您的总体结构比问题提示的要复杂。您试图保持输入的“运行总和”,直到达到给定值为止。考虑以下结构:
laczny = 0
while laczny < 8:
wybor = int(input("Take a number: "))
laczny += wybor
print("Now laczny have: ", laczny)
# work on wybory 0, 1, 2, 3, ...
print("END")
没有类,没有递归,没有从中间退出。 我希望对您来说更容易。