-6

I have written some code for a piece of work i need to do but when i run the code it says

File "/home/ubuntu/workspace/Odeon/153670.py", line 24
    age = input("Age or Type: ")
    ^
IndentationError: expected an indented block

I was wondering if anyone can help ammend this please.

PART of the code where the error is happening is listed below. age = input("Age or Type: ")

print ("Please Input Your age or type.")

while True:
 age_type = None
 int_count = 0
 peak_flag = None
 int_age = 0
 totalprice = 0
 while True:
**age = input("Age or Type: ")** This line brings the error
 if unpeak_price_list.keys().__contains__(age.lower()):
 age_type = age.lower()
 break
 try:
 int_age = int(age)
 if int_age < 2 and int_age > 130:
 print("Please Input Correct age.")
 continue
 break
 except:
 print("Please Input Correct age or type")
TemporalWolf
  • 7,727
  • 1
  • 30
  • 50
  • 7
    Please format your question. Also, how are we supposed to help with an *indentation error* when you neglected all indentation while copying/pasting – jacoblaw Aug 02 '17 at 16:36
  • Probably a mix of tabs and spaces. [Possible duplicate](https://stackoverflow.com/questions/4446366/why-am-i-getting-indentationerror-expected-an-indented-block) – dguay Aug 02 '17 at 16:40
  • try learning the very basics. In Python, indent is used to separate blocks of code. The line you set in bold after the while should be indented, being in the `while` block. – Jacquot Aug 02 '17 at 16:40
  • you're going to get this error several more times. this is just the first instance the compiler hits. check you indentations carefully – Jason Stein Aug 02 '17 at 17:07
  • Possible duplicate of [Why am I getting "IndentationError: expected an indented block"?](https://stackoverflow.com/questions/4446366/why-am-i-getting-indentationerror-expected-an-indented-block) – Brian J Aug 02 '17 at 19:20

4 Answers4

1

Indentation, Python doesn't have brackets like other languages to denote blocks of code, instead it has indentations.

print ("Please Input Your age or type.")

while True:
   age_type = None
   int_count = 0
   peak_flag = None
   int_age = 0
   totalprice = 0
   while True:
       age = input("Age or Type: ")** This line brings the error
       if unpeak_price_list.keys().__contains__(age.lower()):
           age_type = age.lower()
           break
       try:
           int_age = int(age)
           if int_age < 2 and int_age > 130:
               print("Please Input Correct age.")
               continue
           break
       except:
           print("Please Input Correct age or type")
Salmaan P
  • 817
  • 1
  • 12
  • 32
0

Python does not use '{}' to start or to finish a function or flow control structures as other interpreted or compiled languages, it uses the ':' and the indentation typically 4 spaces, every time a ':' is set you need to put 4 spaces more and if the code inside that flow control structure is finished you need to go back 4 spaces, for example:

if a is '':
    a = "Hello word" # Here we add 4 spaces because you start an if 
    if b == 4:
        a += str(b) # Here we add 4 spaces because you start an if 
    else:           # Go back 4 spaces because the last if was finished
        b += 1

same case for the while and every other flow control structure:

while statement_1:
    a += 1
    while statement_2:
        b += 2

if you are already aware of this and you are following what is written above, the error could be that your text editor is using tabs instead of spaces, the text editor that are used for coding usually have a future that changes the tab for spaces, you could enable it.

adrianzx
  • 21
  • 2
0

The problem with this code is that the different blocks of code are not well structured, in other languages, when you use a loop for example to start and end it you use {}, in Python you have to use tabs like this:

while True:
    print('Hello World!')
etc..
...

The same happens to all your code, you have to structure it using tabs. :)

prossellob
  • 779
  • 7
  • 13
0

Here's a fixed version of your code:

print ("Please Input Your age or type.")

while True:
    age_type = None
    int_count = 0
    peak_flag = None
    int_age = 0
    totalprice = 0
    while True:
        age = input("Age or Type: ")
        if unpeak_price_list.keys().__contains__(age.lower()):
            age_type = age.lower()
            break
    try:
        int_age = int(age)
        if int_age < 2 and int_age > 130:
        print("Please Input Correct age.")
        continue
        break
    except:
        print("Please Input Correct age or type")

You need to make sure you're indenting inside a while loop, if statement and try/except statements. I would recommend using larger indents as well to make your code more readable

hhaefliger
  • 521
  • 3
  • 18