Python Forum
name error with text based rpg code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
name error with text based rpg code
#1
I am getting a NameError in my text based rpg and I was wondering if anyone could help. Here's the code:

def chooseCoffee ():
    coffee = ""
    while coffee != "iced coffee" and coffee != "black coffee" and coffee != "cappuccino": # input validation
        coffee = input("Choose your coffee (iced coffee, black coffee, or cappuccino): ")


    return coffee
chooseCoffee()
if(coffee == "iced coffee"):
    print('Cashier: "Iced coffee it is!"')
Here's the error I get:

Error:
if(coffee == "iced coffee"): NameError: name 'coffee' is not defined
My goal is to get the program to print the message "(coffee) it is!"
If there's any way you could help me, I'd really appreciate it! Thanks
Reply
#2
 if(coffee == "iced coffee"):
NameError: name 'coffee' is not defined
Reply
#3
The variable coffee is defined inside the chooseCoffee() function. You can't refer to that variable outside the function. If you want to use information returned from the function you need to assign it to a variable. You could even make that variable coffee, but I'm going to use a different name to show it instead.

mycoffee = chooseCoffee()      # information stored in mycoffee in this section of code
if mycoffee == "iced coffee":  # don't need parenthesis here
    print('Cashier: "Iced coffee it is!"')
cris_ram415 likes this post
Reply
#4
Awesome! Thanks so much! I now have a different problem, however. Even after using a valid input, it repeats the function twice. Here's my updated code:

def chooseCoffee ():
    coffee = ""
    while coffee != "iced coffee" and coffee != "black coffee" and coffee != "cappuccino": # input validation
        coffee = input("Choose your coffee (iced coffee, black coffee, or cappuccino): ")

    return coffee
chooseCoffee()
coffee = chooseCoffee()
if coffee == "iced coffee":
    print('Cashier: "Iced coffee it is!"')
elif coffee == "black coffee":
    print('Cashier: "Black coffee it is!')
elif coffee == "cappuccino":
    print('Cashier: "Cappuccino it is!')
Reply
#5
On line 7 you run the function (but do nothing with the return value).
On line 8 you run the function again (and this time save the return value in "coffee")
cris_ram415 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Color a table cell based on specific text Creepy 11 1,828 Jul-27-2023, 02:48 PM
Last Post: deanhystad
  select Eof extension files based on text list of filenames with if condition RolanRoll 1 1,475 Apr-04-2022, 09:29 PM
Last Post: Larz60+
  Extracting Specific Lines from text file based on content. jokerfmj 8 2,860 Mar-28-2022, 03:38 PM
Last Post: snippsat
  Extract text based on postion and pattern guddu_12 2 1,580 Sep-27-2021, 08:32 PM
Last Post: guddu_12
  A text-based game [SOLVED] Gameri1 6 3,845 Apr-20-2021, 02:26 PM
Last Post: buran
  Winning/Losing Message Error in Text based Game kdr87 2 2,927 Dec-14-2020, 12:25 AM
Last Post: bowlofred
  cx_Oracle.DatabaseError: Error while trying to retrieve text from error ORA-01804 rajeshparadker 0 8,594 Nov-12-2020, 07:34 PM
Last Post: rajeshparadker
  Split gps files based on time (text splitting) dervast 0 1,851 Nov-09-2020, 09:19 AM
Last Post: dervast
  Extracting data based on specific patterns in a text file K11 1 2,180 Aug-28-2020, 09:00 AM
Last Post: Gribouillis
  Read Multiples Text Files get specific lines based criteria zinho 5 3,051 May-19-2020, 12:30 PM
Last Post: zinho

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020