i beginner python. have school project on making program can produce song "99 bottles of beer on wall"
i ask how make show error statement when input non integer value (a string, example).
and how can avoid error:
traceback (most recent call last): file "c:\users\skyfi\desktop\intro com. prog. notes\chapter 11\zheng_tianyu_assignment4_part1.py", line 42, in <module> solution.song() file "c:\users\skyfi\desktop\intro com. prog. notes\chapter 11\zheng_tianyu_assignment4_part1.py", line 9, in song while int(i) > 0: valueerror: invalid literal int() base 10: 'no more'
thank help!
def __init__(self): self.num = input("input number of beer: ") def song(self): = int(self.num) if == 0: print("no more beers on wall.") while int(i) > 0: in range(i, 0, -1): bottle = "bottles" if == 1: bottle = "bottle" if >= 0: print("{0} {1} of beer on wall, {0} {1} of beer.".format(i, bottle)) = - 1 if == 1: bottle = "bottle" if == 0: = str(i) = "no more" bottle = "bottles" print("take 1 down, pass around, {0} {1} of beer on wall.".format(i, bottle)) print(" ") if < 0: print("invalid input!") solution = assignment4part1() solution.song()
first, don't need this:
while int(i) > 0: ^^^
your i
int.
if want catch errors failed integer coversions, need wrap conversion in try
/except
block:
try: = int(self.num) except valueerror: # error handling code goes here # note: have no value assigned.