Home > OS >  I have a decently simple question for you
I have a decently simple question for you

Time:12-02

I'm new to coding and I was wondering why I'm getting an error with it not knowing the variable "Meds", when it was identified previously.

I was wondering if anyone could tell me why? It says Meds is undefined, but it is defined above. I tried putting the problem code above and below where meds if defined originally, but it still won't work.

Code:

#Vals

money = 700
Gun1 = 0
MilesLeft = 20

#def wannastart():
  #realstart = (int(input("Type 1 To Begin,2 for info on the game, and 3 for a secret:  ")))

#This code starts the store  
def store():
  print("Welcome to the Store, this is where you will buy everything you need for your `road trip!")`
  print("How many Blankets would you like to buy? The cost is 30 dollars per blanket, and you have "   str(money)   " dollars left!")
  many1 = int(input("Amount: "))
  Blankets = 0   many1
  sub1 = many1 * 30
  r = money - sub1
  print("You have "   str(r)   " dollars left!")
  print("How many Spare Tires would you like to buy? The cost is 50 dollars per Tire, and you have "   str(r)   " dollars left!")
  many2 = int(input("Amount: "))
  Tires = 0   many2
  sub2 = many2 * 50
  b = r - sub2
  print("You have "   str(b)   " dollars left!")
  print("Would you like to buy Meds? It costs 40 dollars per")
  many3 = int(input("Amount: "))
  #I defined meds here
  Meds = 0   many3
  sub3 = many3 * 40
  n = b - sub3
  print("You have "   str(n)   " dollars left!")
  print("Would you like to buy a gun? The cost is 100 dollars")
  yn1 = input("Y/N: ")
  if yn1 == "Y":
    Gun = Gun1   1
    j = n - 100
    game()
  if yn1 == "N":
    print("Ok, don't then :(")
    j = r - 0
    money - j
    
    game()

def game():
  
  from random import seed
  from random import randint
  for _ in range(1):
      value = randint(1, 1)
      print("You Rolled a : "  str(value))
  if value == 1:
    for _ in range(1):
      suber = randint(0,5)
      Miles = MilesLeft - suber
    print("Your Family Travels "   str(suber)   " Miles, one step closer to the hotel") 
    print("Your Family Has "   str(Miles)   " Miles Left!")
    game()
  elif value == 2:
    print("Oh no! One of your family members has caught Covid-19!")
    print("If you have meds, you can cure your family! If not, Your Family will perish!")
    #Error Here, can't identify Meds
    if Meds < 1:
      print("Oh no! You Died!")

#Start of the game
def start():
  print("Welcome to the Road Trip!")
  realstart = (int(input("Type 1 To Begin,2 for info on the game, and 3 for a secret:  ")))
  if realstart == 3:
  #heh get rickrolled
    print ("Never Gonna Give You Up")
    print ("Rick Astley")
    print ("Were no strangers to love")
    print ("You know the rules and so do I")
    print ("A full commitments what I'm thinking of")
    print ("You wouldn't get this from any other guy")
    print ("I just wanna tell you how I'm feeling")
    print ("Gotta make you understand")
    print ("Never gonna give you up")
    print ("Never gonna let you down")
    print ("Never gonna run around and desert you")
    print ("Never gonna make you cry")
    print ("Never gonna say goodbye")
    print ("Never gonna tell a lie and hurt you")
    print ("We've known each other for so long")
    print ("Your hearts aching but you're too shy to say it")
    print ("Inside we both know whats been going on")
    print ("We know the game and were gonna play it")
    print ("And if you ask me how I'm feeling")
    print ("Don't tell me you're too blind to see")
    print ("Never gonna give you up")
    print ("Never gonna let you down")
    print ("Never gonna run around and desert you"   )
    print ("Never gonna make you cry")
    print ("Never gonna say goodbye" )
    print ("Never gonna tell a lie and hurt you")
    print ("Never gonna give you up")
    print ("Never gonna let you down")
    print ("Never gonna run around and desert you")
    print ("Never gonna make you cry")
    print ("Never gonna say goodbye")
    print ("Never gonna tell a lie and hurt you")
    print ("Never gonna give, never gonna give")
    print ("(Give you up)")
    print ("(Ooh) Never gonna give, never gonna give")
    print ("(Give you up)")
    print ("We've known each other for so long"   )
    print ("Your hearts been aching but you're too shy to say it")
    print ("Inside we both know whats been going on")
    print ("We know the game and were gonna play it")
    print ("I just wanna tell you how I'm feeling")
    print ("Gotta make you understand")
    print ("Never gonna give you up")
    print ("Never gonna let you down")
    print ("Never gonna run around and desert you")
    print ("Never gonna make you cry")
    print ("Never gonna say goodbye")
    print ("Never gonna tell a lie and hurt you")
    print ("Never gonna give you up")
    print ("Never gonna let you down")
    print ("Never gonna run around and desert you")
    print ("Never gonna make you cry")
    print ("Never gonna say goodbye")
    print ("Never gonna tell a lie and hurt you")
    print ("Never gonna give you up")
    print ("Never gonna let you down")
    print ("Never gonna run around and desert you")
    print ("Never gonna make you cry")
    print ("Get RickRolled lol")
  elif realstart == 2:
    print("This is pretty much like the Oregon trail, but a bit different, with `different minigames")`
    start()
  elif realstart == 1:
    store()
  else:
    print("Not a valid number, ya bozo!")


start()

CodePudding user response:

To me it looks like you're trying to implement OOP (object oriented programming) principles in your code, but without actually doing any OOP.

Without getting into an extended Python OOP guide in this answer, in order to use a separate store function that uses code like your one you would need to declare a Player class with the attributes Meds, Tires, Guns, money, etc and then instantiate that class at the start of your program. You can then pass the Player object from the game function to the store function and update the object (likely using methods defined within the class).

This kind of game is exactly the kind of example that is used in a lot of beginner Python OOP courses (search "python oop rpg tutorial"). I don't know whether you want to get into this relatively complicated way learning python, but from how you've written your code I think that learning OOP may click well with you.

If you didn't want to use OOP style code then you would need to modify your code quite a lot.

CodePudding user response:

It's simple bro, just move your game function above store function :)

  • Related