0

I’m finding the Python syntax very confusing, mainly concerning variables. I’m trying to learn it using the Microsoft EDX course but I’m struggling when trying to check if a string from an input is in the variable.

Example 1: Check if a flavor is on the list

# menu variable with 3 flavors

def menu (flavor1, flavor2, flavor3): 
    flavors = 'cocoa, chocolate, vanilla'
    return menu

# request data from the user flavorName = input('What flavor do your want? ')
data = input("What flavor do you want? ")

#print the result

print ("It is", data in menu, "that the flavor is available")

Example 2: Print a message indicating name and price of a car

def car (name, price):
     name = input(“Name of the car: “)
     price = input (“Price of the car: “)
     return (name, price)

print (name, “’s price is”, price)

Also, I would like to know what would be the disadvantage of doing something like this for the example 2:

name = input("name of the car: ")  
price = input ("price of the car: ") 
print (name,"is", price,"dollars")

Could someone please clarify this to me? Thank you very much!

3
  • 1
    are you sure your examples are working? Commented Oct 15, 2018 at 1:47
  • I'm not sure I understand your question. What exactly is unclear, or what is not working the way you expect? Commented Oct 15, 2018 at 2:14
  • @hsnsd Ex 1 and 2 show what I'm trying to do to accomplish the task. I'm struggling because they are not working... @larsks Hi! I'm not understanding how to access the string output and check it using the in in the variable defined at the beginning (I receive the error that it is not possible to interact with the variable). Commented Oct 15, 2018 at 3:32

3 Answers 3

1

i didnt understand what your trying to do in first example.

But i can partially understand what your trying to do in second example,

def car ():
     name = input("Name of the car: ")
     price = input ("Price of the car: ")
     return name, price

name,price = car()

print ("{}\'s price is {}".format(name,price))

the above code is the one of the way to solve your problem,

python can return multiple variable

use format function in print statement for clean display.

You dont need function parameters for car. since your taking input from in car function and returning it to the main.

Hope it helps you understand.

Sign up to request clarification or add additional context in comments.

Comments

0

Example 1

# menu variable with 3 flavors



def menu():
    flavors = 'cocoa, chocolate, vanilla'
    return flavors #return flavors instead of menu 


# request data from the user flavorName = input('What flavor do your want? ')
data = input("What flavor do you want? ")

# print the result

print ("It is", data in menu(), "that the flavor is available") #menu is a function so invoke with menu () instead of menu 

Example 2:

def car(): #no input required since you are getting the input below
    name = input('Name of the car: ')
    price = input('Price of the car: ')
    return (name, price)


name, price = car() #call the function to return the values for name and price  
print (name, "’s price is", price)

The below approach works and is faster as compared to calling the function although adding small stuff to form functions allows the program to be modularized, making it easier to debug and reprogram later as well as better understanding for a new programmer working on the piece of code.

name = input("name of the car: ")
price = input("price of the car: ")
print (name, "is", price, "dollars")

Comments

0

Just found how to print the result in the way the exercise required. I had difficulty explaining, but here it is an example showing:

def car(name,price):
    name_entry = input("Name car: ")
    price_entry = input("Price car: ")
    return (name_entry,price_entry)

This is the way to print the input previously obtained

print (car(name_entry,price_entry))

Thank you very much for all the explanations!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.