Hello there! I’m trying to expand on the python 3 8-ball project by adding a input() method to the name variable so that i can enter in any name. When I run the final project i get the name i wanted but my answer is printed right under it instead of in the “magic 8-ball’s answer:” spot. What part of my code should i take a look at?
Hi There,
In your code example, random_integer selected 7
and printed the output, “Yes…yes you are”, however, the variable answer was never assigned to anything expect an empty string. Therefore, you need to assign the variable answer to one of the output messages. For example:
import random
name = input("Enter your name: ")
answer = ''
random_integer = random.randint(1, 10)
if random_integer == 1:
answer = 'Naaah'
elif random_integer == 2:
answer == 'You eat a bowl of code'
# --- Continue the pattern:
else:
answer = 'Error'
print(name, answer)
Now, when a random_integer is selected, that particular message will be assigned to the answer variable, and will display in the output. Let me know if this resolves your issue!
It’s giving me the answer before the name and then the answer afterwards. What im trying to do is enter in any name i want and then have it answer my question with the specified name.
I apologize, I mistyped something in my previous post and I cannot edit. The correct code should go something like this:
import random
name = input("Enter your name: ")
question = "Am I tough enough"?
answer = ''
random_integer = random.randint(1, 10)
if random_integer == 1:
answer = 'Naaah'
elif random_integer == 2:
answer = 'You eat a bowl of code'
# --- Continue the pattern:
else:
answer = 'Error'
print(f"{name} ask {question}")
print(f"Magic 8-ball's answer: {answer}")
Again, I apologize for any confusion. Let me know how that works!
That kind of worked for me! Im still having trouble figuring out how to get 3 and 4 to switch places though. I’ve tried rearranging the code but that doesn’t seem to work for me.
Hey There,
I’m glad you’re to hear this almost worked for you! If you feel comfortable, would you mind positing your code (not a screen shot)? Look forward t your response!
import random
name = input(“Enter your name:”)
question = “Am I tough enough?”
random_integer = random.randint(1, 10)
answer = “”
if random_integer == 1:
print(“Naaaah”)
elif random_integer == 2:
print(“You eat a bowl of nails w/o any milk?”)
elif random_integer == 3:
print(“How many bones haven’t you broken?”)
elif random_integer == 4:
print(“Try again…”)
elif random_integer == 5:
print(“As tough as a creme puff”)
elif random_integer == 6:
print(“LoL!”)
elif random_integer == 7:
print(“Yes…yes you are”)
elif random_integer == 8:
print(“rolls eyes”)
elif random_integer == 9:
print(“Drop and gimme fitty”)
elif random_integer == 10:
print(“crickets”)
else:
default = “Error”
#print(name, “asks:”, question)
#print(“Magic 8-ball’s answer:”, answer)
print(f"{name} asks {question}")
print(f"Magic 8-ball’s answer: {answer}")
Certainly! thank you for helping out!
import random
random_integer = random.randint(1, 10)
# Get user's name
name = input("Enter your name:\n> ")
answer = ''
while True: # start main loop
# Get user to ask a question
question = input("Ask your question:\n> ")
if random_integer == 1:
answer = "Naaaah"
elif random_integer == 2:
answer = "You eat a bowl of nails w/o any milk?"
elif random_integer == 3:
answer = "How many bones haven’t you broken?"
elif random_integer == 4:
answer = "Try again…"
elif random_integer == 5:
answer = "As tough as a creme puff"
elif random_integer == 6:
answer = "LoL!"
elif random_integer == 7:
answer = "Yes…yes you are"
elif random_integer == 8:
answer = "rolls eyes"
elif random_integer == 9:
answer = "Drop and gimme fitty"
elif random_integer == 10:
answer = "crickets"
print(f"{name} asks {question}")
print(f"Magic 8-ball’s answer: {answer}\n")
Change Log
- Added a while loop for continuous play
- Added input() to question to allow user to ask questions.
- Removed the final else statement
- Since you’re using randint(1, 10) there will never be an output variable beyond this range
If you have any questions, please do not hesitate to ask!
Best regards,
Yes that did it! It outputs in the order that I want but it only prints answer 5, I’m trying to understand why the code is doing that?
Move:
random_integer = random.randint(1, 10)
With in the While Loop:
while True: # start main loop
random_integer = random.randint(1, 10)
# Get user to ask a question
question = input("Ask your question:\n> ")
Let me know if that resolves the issue!
You are the best! That worked perfect thank you, i have a lot to learn lol.
#Vending Machine
#Creating a VendingMachine function
def VendingMachine():
#Display text
print("\nWelcome to the Vending Machine!\n--------------------")
#Creating categories
#Creating Drinks Category:
a={
"item_id":"1",
"item_name":"Pepsi",
"item_price":3.00,
"stock":5
},
b={
"item_id":"2",
"item_name":"Sprite",
"item_price":3.00,
"stock":5
},
c={
"item_id":"3",
"item_name":"Fanta",
"item_price":3.00,
"stock":5
},
d={
"item_id":"4",
"item_name":"Coffee",
"item_price":2.00,
"stock":5
},
e={
"item_id":"5",
"item_name":"Tea",
"item_price":2.00,
"stock":5
},
f={
"item_id":"6",
"item_name":"Juice",
"item_price":1.50,
"stock":5
},
g={
"item_id":"7",
"item_name":"Water",
"item_price":1.00,
"stock":5
},
h={
"item_id":"8",
"item_name":"Milk",
"item_price":2.00,
"stock":5
},
#Creating Snacks Category:
i={
"item_id":"9",
"item_name":"Chips",
"item_price":2.50,
"stock":5
},
j={
"item_id":"10",
"item_name":"Cookie",
"item_price":2.00,
"stock":5
},
k={
"item_id":"11",
"item_name":"Chocolate",
"item_price":4.00,
"stock":5
},
l={
"item_id":"12",
"item_name":"Apple Pie",
"item_price":4.50,
"stock":5
},
m={
"item_id":"13",
"item_name":"Chips",
"item_price":2.50,
"stock":5
},
n={
"item_id":"14",
"item_name":"Nut Bar",
"item_price":1.50,
"stock":5
},
o={
"item_id":"15",
"item_name":"Croissant",
"item_price":2.50,
"stock":5
},
p={
"item_id":"16",
"item_name":"Sandwich",
"item_price":3.50,
"stock":5
},
q={
"item_id":"17",
"item_name":"Popcorn",
"item_price":2.00,
"stock":5
},
r={
"item_id":"18",
"item_name":"Chocolate Cake",
"item_price":4.00,
"stock":5
},
s={
"item_id":"19",
"item_name":"Strawberry Cake",
"item_price":4.00,
"stock":5
},
t={
"item_id":"20",
"item_name":"Vanilla Cake",
"item_price":4.00,
"stock":5
}
#Creating a list which contains all The items
AvailableItems = [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t]
#Cash in Machine:
Cash = 0
#Creating another function to show the prices
def display(AvailableItems):
print("\nThe items available are: \n")
for i in t:
print(i["item_id"], i["item_name"], i["item_price"])
print("\n--------------------\n")
display(t)
VendingMachine()
hey everyone! i want to display these properties of dictionary but idk why its not happening can anyone please help?
You have a comma ,
between what you intended to be dictionary a
and dictionary b
and so on.
This would make a
become a tuple (with [a] dictionary inside it)
You can remove all these outer commas to prevent this from happening.
Also, you seem to have mixed up the argument AvailableItems
with the parameter t
at the end.
In the definition/declaration of the display
function, you’re looping through t
,
so t
should be the parameter instead of AvailableItems
And want to use that function to display prices for stuff in AvailableItems
so display should be called with an argument of AvailableItems
instead of t
at the end.
Meaning
display(t)
should be
display(AvailableItems)
An alternative is to have a dictionary of dictionaries.
for_sale = {
'a': {
"item_id":"1",
"item_name":"Pepsi",
"item_price":3.00,
"stock":5
},
'b': {
"item_id":"2",
"item_name":"Sprite",
"item_price":3.00,
"stock":5
},
# and so on ...
}