Тоді тут же питання задам, щоб тему нову не робити.Пишу інтерпретатор для HQ9+(https://uk.wikipedia.org/wiki/HQ9+):
code = input("CODE: ")
count = 0
for k in code:
if k == "h": print("Hello World!")
elif k == "q": print(code)
elif k == "9":
for beer in range(99, 1, -1):
print(beer, "bottles of beer on the wall,", beer, "bottles of beer.")
print("Take one down and pass it around,", beer-1, "bottles of beer on the wall.\n")
print("1 bottle of beer on the wall, 1 bottle of beer.")
print("Take one down and pass it around, no more bottles of beer on the wall.\n")
print("No more bottles of beer on the wall, no more bottles of beer.")
print("Go to the store and buy some more, 99 bottles of beer on the wall.")
elif k == "+": count += 1
else: print("Syntax error")
input("Enter...")
Не працює.Брав приклад з Вікіпедії:
cnt = 0
source = input()
for c in source:
if c == 'H':
print('Hello world!')
elif c == 'Q':
print(source)
elif c == '9':
for beer in range(99, 1, -1):
print(beer, 'bottles of beer on the wall,', beer, 'bottles of beer.')
print('Take one down and pass it around,', beer-1, 'bottles of beer on the wall.')
print()
print('1 bottle of beer on the wall, 1 bottle of beer.')
print('Take one down and pass it around, no more bottles of beer on the wall.')
print()
print('No more bottles of beer on the wall, no more bottles of beer.')
print('Go to the store and buy some more, 99 bottles of beer on the wall.')
elif c == '+':
cnt += 1
else:
print('Syntax error')
input()
Цей працює.Я просто хочу зрозуміти як воно все працює і переписую, змінюю.
Наскільки зрозумів у моїй програмі вся проблема у коді
for beer in range(99, 1, -1):
print(beer, "bottles of beer on the wall,", beer, "bottles of beer.")
print("Take one down and pass it around,", beer-1, "bottles of beer on the wall.\n")
print("1 bottle of beer on the wall, 1 bottle of beer.")
print("Take one down and pass it around, no more bottles of beer on the wall.\n")
print("No more bottles of beer on the wall, no more bottles of beer.")
print("Go to the store and buy some more, 99 bottles of beer on the wall.")
Але що саме не так?