1

I want to write a small script to tell me if the bass level is OK or not from user input.

I am just learning user input, and this is what I have so far:

def crisp():
    bass = input("Enter bass level on a scale of 1 to 5>>")
    print ("Bass level is at") + bass
    if bass >=4:
       print ("Bass is crisp")    
    elif bass < 4:
       print ("Bass is not so crisp")
Ben Morris
  • 606
  • 5
  • 24
jahrich
  • 23
  • 1
  • 1
  • 4

3 Answers3

3

When you take in input() through the built-in function, it takes input as a string.

>>> x = input('Input: ')
Input: 1
>>> x
"1"

Instead, cast int() to your input():

>>> x = int(input('Input: '))
Input: 1
>>> x
1

Otherwise, in your code, you are checking if "4" == 4:, which is never true.

Thus, here is your edited code:

def crisp():
    bass = int(input("Enter bass level on a scale of 1 to 5>>"))
    print ("Bass level is at") + bass
    if bass >=4:
       print ("Bass is crisp")    
    elif bass < 4:
       print ("Bass is not so crisp")
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
  • 2
    If you are using Python 2.x, [don't use `input`, use `raw_input` always](http://stackoverflow.com/a/7710959/786020). – Poik Feb 06 '15 at 16:42
  • Yes, @BenMorris, but in [tag:python-2.x] I would suggest using `raw_input()` :) – A.J. Uppal Feb 06 '15 at 16:43
1

Convert to an integer:

bass = int(input("Enter bass level on a scale of 1 to 5>>"))
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70
0

I really don't see a problem here, but just a simple program that does this and only this would be as so:

a=1
while a==1:
    try:
        bass = input('Enter Bass Level: ')
        print('Bass level is at ' + str(bass))
        if bass >=4:
            print("Bass is crisp")
        elif bass < 4:
            print('Bass is not so crisp')
        a=0
    except ValueError:
        print('Invalid Entry')
        a=1

Not much difference with a function:

def Bass():
    a=1
    while a==0:
        try:
            bass = input('Enter Bass Level: ')
            print('Bass level is at ' + str(bass))
            if int(bass) >=4:
                print("Bass is crisp")
            elif bass < 4:
                print('Bass is not so crisp')
            a=0
    except ValueError:
        print('Invalid Entry')
        a=1
Ben Morris
  • 606
  • 5
  • 24
  • 2
    Your code is with Python 2.x in mind. In Python 3.x, `raw_input` from 2.x is the default behavior of `input` in 3.x. Considering the title mentions 3.4, this answer is technically wrong, but still possibly helpful. For future viewers: **use `raw_input` instead of `input` in Python 2.x code** for security reasons. – Poik Feb 06 '15 at 16:28
  • Thanks for the explanations. I have not used "try" yet. I will check it out. – jahrich Feb 06 '15 at 16:41
  • Very useful. as I am learning in 2 and using a 3 interpreter of IDLE. – jahrich Feb 06 '15 at 16:43
  • @jahrich this answer is inaccurate, first, the indentation is off. Second, **you cannot catch a SyntaxError that way in [tag:python-3.x]** – A.J. Uppal Feb 06 '15 at 16:46
  • @A.J. I corrected SyntaxError to ValueError and fixed the bass var. for 3.4. – Ben Morris Feb 06 '15 at 17:29
  • @jahrich please note that in 3.4 print is a function and must be used as print(text, endline char) and cannot have anything outside of the parentheses, or it gives an error. – Ben Morris Feb 06 '15 at 17:32