T
T
tarroma92018-10-19 19:57:37
Python
tarroma9, 2018-10-19 19:57:37

How to build a loop in python without error?

Please tell me how to make a loop in a simple python program.

usd = 65.59
euro = 75.37



new_exchange = []



money = int (input("Введите сумму, которую хотите обменять в валюту    "))
exchange = input ("Выберите валюту (USD или Euro)   ")


while new_exchange == ():
  if exchange.lower() == "usd":
  result_usd = money * usd
  print("После обмена вы получите", result_usd, "долларов сша")

  elif exchange.lower() == "euro":
  result_euro = money * euro
  print("После обмена вы получите", result_euro, " евро")
  
  new_exchange = input ("Хотите снова обменять?? ")
  if new_exchange.lower == "да"


print("Работа программы завершена! ")

As a result, it gives an error:
File "C:\python\test.py", line 17
    result_usd = money * usd
             ^
IndentationError: expected an indented block

If you do it without a loop, then everything works:
usd = 65.59
euro = 75.37

money = int (input("Введите сумму, которую хотите обменять в валюту    "))
exchange = input ("Выберите валюту (USD или Euro)   ")


if exchange.lower() == "usd":
  result_usd = money * usd
  print("После обмена вы получите", result_usd, "долларов сша")

elif exchange.lower() == "euro":
  result_euro = money * euro
  print("После обмена вы получите", result_euro, " евро")
  
print("Работа программы завершена! ")

Result:
Введите сумму, которую хотите обменять в валюту    4
Выберите валюту (USD или Euro)   euro
После обмена вы получите 301.48  евро
Работа программы завершена!

I'm still new to python, just learning the while loop. And in this case, it is necessary that the program asks whether to continue again and, if so, restart the loop. But only a new value was added to the result. Perhaps this is an obvious mistake, but, again, I just started learning this language ..

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Mirinum, 2018-10-19
@tarroma9

In this program, you don’t count how many dollars or euros you get, you count the other way around - how many rubles you get for N currencies
. Since you need the program to work, it will work with this code

usd = 0.01524622655
euro = 0.01326787846

def exchng():
  money = int(input("Введите сумму, которую хотите обменять в валюту   "))
  exchange = input("Выберите валюту (USD или Euro)   ")
  if exchange.lower() == "usd":
    result_usd = money * usd
    print("После обмена вы получите", result_usd, "долларов сша")
  elif exchange.lower() == "euro":
    result_euro = money * euro
    print("После обмена вы получите", result_euro, " евро")

new_exchange = 1

while new_exchange == 1:
  exchng()
  rep = input("Хотите снова обменять?? (Y/N)   ")
  if(rep.upper() == "N"):
    print("Работа программы завершена! ")
    new_exchange = 0
    break

K
Keste ..., 2018-10-19
@Keste

Error in indents (You need to add indents after ifs in your loop).
Python is case and indentation sensitive. - This is his main feature!
One indent is 4 spaces or one tab, if you use it this way and that, it will also be an error!
PS I know that I'm not the first to answer but I decided to answer!

B
bbkmzzzz, 2018-10-19
@bbkmzzzz

IndentationError: expected an indented block <- indentation error. Python is case and indentation sensitive. Blocks must be indented, by default 4 spaces. Do not use tabs:

x = 5
if x == 10:
    print('x == 10')
elif x == 3:
    print('x == 3')
else:
    print('x что-то странное')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question