0

I'm a beginner in coding and started because it's part of my course. I tried to make a function called displayOptions responsible for displaying options to the user and asking them for input from 1 to 7. Input such as string or numbers above 7 or below 1 should be rejected and the error message should be displayed.

def displayOptions():
#This part of the function is supposed to just print, and it's working correctly.

print ("Please select one of the following options:"), print ("1. Teaspoon to metric"), print ("2. Tablespoon to metric"), print ("3. Fluid ounces to metric"), print ("4. Pint to metric"), print ("5. Display list"), print ("6. Help"), print ("7. Close")


#Here I tried to make a loop until the user picks a number from 1 to 7 however the loop doesn't stop when the numbers are entered.

while True: 
    int(input) == (1, 7)
    break
    print("this is not a valid input, try to use a number from 1 to 7")
    pass
  pass
DaWie
  • 3
  • 2
  • Note: When posting Python code, please take care that the indentation is preserved. – Christoph Burschka Mar 12 '19 at 14:09
  • Welcome to StackOverflow. When pasting code into your posts, the best way to ensure that proper formatting is maintained is to immediately select the code you just pasted and either type Ctrl+K or click the `{}` button in the interface. Otherwise, you risk introducing indentation errors like this by trying to manually "fix" the formatting. – glibdud Mar 12 '19 at 14:09

3 Answers3

1

It seems like there are a few mistakes in your code. First of all, make sure your indentation is correct. See commented code for more explanation.

def displayOptions():
  # there is an easier way to print your whole list: the escape char \n starts a new line.
  print("Please select one of the following options:\n1. Teaspoon to metric\n2. Tablespoon to metric\n3. Fluid ounces to metric\n4. Pint to metric\n5. Display list\n6. Help\n7. Close")

  while True: 
    # you are asking the user for input. 
    inp = input('') # Optionally, you're instructions could also be inside the input() function.
    if int(inp) < 1 or int(inp) > 7: # you have to check whether the input is between 1 and 7 with an if condition
      print("this is not a valid input, try to use a number from 1 to 7")
    else: break # I don't know why you used pass all the time. But a break here in the else statement does the job as it exits the loop when the input is between 1 and 7.

displayOptions()  
trotta
  • 1,232
  • 1
  • 16
  • 23
0
int(input) == (1, 7)

The expression you want here is

1 <= int(input) <= 7

and it should be part of an if statement.

(There are some other issues, but they're mostly indentation related so it's not possible to tell if they're in the code or just this post.)

Christoph Burschka
  • 4,467
  • 3
  • 16
  • 31
0

As you probaby know, Python is strict to indentation, so the issue with your code could be that. Anyway there is a cleaner way to do it:

def displayOptions():
    print ("Please select one of the following options:"), print ("1. Teaspoon to metric"), print ("2. Tablespoon to metric"), print ("3. Fluid ounces to metric"), print ("4. Pint to metric"), print ("5. Display list"), print ("6. Help"), print ("7. Close")

    while True: 
        if 1 <= int(input()) <=7:
            break
        else:
            print("this is not a valid input, try to use a number from 1 to 7")
  • Here are some tips you should take in account: If possible use a line per statement not 8 prints in a single line, when you ask for an input value store it or you will have to ask again, `pass` keyword is only necessary when it comes alone. – OSainz Mar 12 '19 at 14:24
  • note that you should indent with 4 spaces in order to follow [PEP 8](https://www.python.org/dev/peps/pep-0008/) – Aemyl Mar 12 '19 at 14:36