user3598308
user3598308

Reputation: 41

Python Choices and input

I'm very new to Python and I need some help with this line of code:

sub = float(input("Type in the name of the sub sandwhich which you'd like to know the nutrition facts of."))

if sub == bacon:
    print("282 Calories, 688.4g Fat, 420mg Sodium, 1407mg Potassium, and 33.5g Protein")

Results after inputing:

ValueError: could not convert string to float: 'bacon

Upvotes: 4

Views: 637

Answers (3)

A.J. Uppal
A.J. Uppal

Reputation: 19264

You have two errors in your code. You are asking the name of the sub sandwich, therefore you do not need to case float(input()), just leave it at input(). Tip: If you are using python2.x, use raw_input() instead of input()

Your second error is that you are checking if sub == bacon:. sub is already defined, but bacon is not, so you need to surround it with quotations.

Here is your edited code:

sub = input("Type in the name of the sub sandwich which you'd like to know the nutrition facts of:\n")

if sub == 'bacon': #'bacon' with quotations around it 
    print("282 Calories, 688.4g Fat, 420mg Sodium, 1407mg Potassium, and 33.5g Protein")

This runs as:

bash-3.2$ python3 test.py
Type in the name of the sub sandwich which you'd like to know the nutrition facts of:
bacon
282 Calories, 688.4g Fat, 420mg Sodium, 1407mg Potassium, and 33.5g Protein
bash-3.2$ 

If you are looking to add more possible subs, I would suggest using a dictionary, like the following:

subs = {'bacon':["282", "688.4", "420", "1407", "33.5"], 'ham':["192", "555.2", "340", "1802", "44.3"], "cheese":["123", "558.9", "150", "1230", "12.5"]}

sub = input("Type in the name of the sub sandwich which you'd like to know the nutrition facts of:\n")

arr = subs[sub]
print("%s Calories, %sg Fat, %smg Sodium, %smg Potassium, and %sg Protein" %(arr[0], arr[1], arr[2], arr[3], arr[4]))

This runs as:

bash-3.2$ python3 test.py
Type in the name of the sub sandwich which you'd like to know the nutrition facts of:
bacon
282 Calories, 688.4g Fat, 420mg Sodium, 1407mg Potassium, and 33.5g Protein
bash-3.2$ python3 test.py
Type in the name of the sub sandwich which you'd like to know the nutrition facts of:
ham
192 Calories, 555.2g Fat, 340mg Sodium, 1802mg Potassium, and 44.3g Protein
bash-3.2$ python3 test.py
Type in the name of the sub sandwich which you'd like to know the nutrition facts of:
cheese
123 Calories, 558.9g Fat, 150mg Sodium, 1230mg Potassium, and 12.5g Protein
bash-3.2$ 

Upvotes: 2

sshashank124
sshashank124

Reputation: 32189

You cannot cast a non number string as a float. Do it as follows:

sub = input("Type in the name of the sub sandwhich which you'd like to know the nutrition facts of.")

if sub == 'bacon':     # 'bacon' not bacon 
    print("282 Calories, 688.4g Fat, 420mg Sodium, 1407mg Potassium, and 33.5g Protein")

Upvotes: 1

TCVM
TCVM

Reputation: 59

You are asking for a string, not a float number. Use input instead of float(input). And in the If statement, put in quotations for the bacon

sub = input("Type in the name of the sub sandwhich which you'd like to know the nutrition facts of.")

if sub == 'bacon': print("282 Calories, 688.4g Fat, 420mg Sodium, 1407mg Potassium, and 33.5g Protein")

Upvotes: 0

Related Questions