Variable behave problem

Is it possible to have a variable that doesn’t reset when you press “boot” again?

Current behavior:
It says that it doesn’t recognize the variables inside the “if” command, but i also need to use them outside the “if” command without changing the data that it has.

Desired behavior
It need to use the variables in the second “if” command to make the game a bit play-abel, without you just buy one thing and it ends. But the current value of the variable can’t change.

Repl link:
https://replit.com/@cuypersmathis/game?v=1

Code
print("1. start new game")
print("2. load prevorious game")
print("3. wat's new?")
print(" ")
opdracht=input()

if opdracht == "1":
  money=int(100)
  rep=int(0)
  bought=int(0)
  sold=int(0)
  apples=int(0)
  bananas=int(0)
  oranges=int(0)
  grapes=int(0)
  
  print(" ")
  print(" ")
  print(" ")
  print("current stats:")
  print("  money: €" + str(money))
  print("  reputation: " + str(rep))
  print("  products bought: " + str(bought))
  print("  products sold: " + str(sold))

  print(" ")
  print(" ")
  print(" ")
  print("choose your next step:")
  print("  1. buy a product")
  print("  2. sell a product")
  print("  3. buy an add-board")
  choise=input()

  if choise == "1":
    print(" ")
    print(" ")
    print(" ")
    print("you have €" + str(money) + " to buy something")
    print("wich of these product do you want to buy?")
    print(" ")
    print("  1. apples:")
    print("    buy price: €1")
    print("    sell price: €2")
    print(" ")
    print("  2. bananas:")
    print("    buy price: €50")
    print("    sell price: €60")
    print(" ")
    print("  3. oranges:")
    print("    buy price: €100")
    print("    sell price: €150")
    print(" ")
    print("  4. grapes:")
    print("    buy price: €500")
    print("    sell price: €600")
    print(" ")

    product=input()

    if product == "1":
      print(" ")
      print(" ")
      print("how many do you want to buy?")
      print("max amout you can afford: " + str(money))
      amount=int(input())
      
      if amount > money:
        print(" ")
        print(" ")
        print("you can't afford that much")
        
      else:
        money=money-(amount*1)
        apples=apples+amount
        bought=bought+amount
        print(" ")
        print(" ")
        print("you have bought " + str(amount) + " apples")
        print("you have €" + str(money) + " left")

    if product == "2":
      print(" ")
      print(" ")
      print("how many do you want to buy?")
      print("max amout you can afford: " + str(money/50))
      amount=int(input())
      
      if amount > money:
        print(" ")
        print(" ")
        print("you can't afford that much")
        
      else:
        money=money-(amount*50)
        bananas=bananas+amount
        bought=bought+amount
        print(" ")
        print(" ")
        print("you have bought " + str(amount) + " bananas")
        print("you have €" + str(money) + " left")

    if product == "3":
      print(" ")
      print(" ")
      print("how many do you want to buy?")
      print("max amout you can afford: " + str(money/100))
      amount=int(input())
      
      if amount > money:
        print(" ")
        print(" ")
        print("you can't afford that much")
        
      else:
        money=money-(amount*100)
        oranges=oranges+amount
        bought=bought+amount
        print(" ")
        print(" ")
        print("you have bought " + str(amount) + " oranges")
        print("you have €" + str(money) + " left")

    if product == "4":
      print(" ")
      print(" ")
      print("how many do you want to buy?")
      print("max amout you can afford: " + str(money/500))
      amount=int(input())
      
      if amount > money:
        print(" ")
        print(" ")
        print("you can't afford that much")
        
      else:
        money=money-(amount*500)
        grapes=grapes+amount
        bought=bought+amount
        print(" ")
        print(" ")
        print("you have bought " + str(amount) + " grapes")
        print("you have €" + str(money) + " left")


  if choise == "2":
    print(" ")
    print(" ")
    print(" ")
    
    if bought == 0:
      print("you haven't bought anything yet")
      
    else:
      print("you have €" + str(money) + " to sell something")
      print("wich of these product do you want to sell?")
      print(" ")
      print("  1. apples:")
      print("    sell price: €2")
      print(" ")
      print("  2. bananas:")
      print("    sell price: €60")
      print(" ")
      print("  3. oranges:")
      print("    sell price: €150")
      print(" ")
      print("  4. grapes:")
      print("    sell price: €600")
      print(" ")
      choise=input()

      if choise == "1":
        print(" ")
        print(" ")
        print("how many do you want to sell?")
        print("max amout you can sell: " + str(apples))
        amount=int(input())
        
        if amount > apples:
          print(" ")
          print(" ")
          print("you can't sell that much")
          
        else:
          money=money+(amount*2)
          apples=apples-amount
          sold=sold+amount
          bought=bought-sold
          print(" ")
          print(" ")
          print("you have sold " + str(amount) + " apples")
          print("you have €" + str(money))

      if choise == "2":
        print(" ")
        print(" ")
        print("how many do you want to sell?")
        print("max amout you can sell: " + str(bananas))
        amount=int(input())
        
        if amount > bananas:
          print(" ")
          print(" ")
          print("you can't sell that much")

        else:
          money=money+(amount*60)
          bananas=bananas-amount
          sold=sold+amount
          bought=bought-sold
          print(" ")
          print(" ")
          print("you have sold " + str(amount) + " bananas")
          print("you have €" + str(money))

      if choise == "3":
        print(" ")
        print(" ")
        print("how many do you want to sell?")
        print("max amout you can sell: " + str(oranges))
        amount=int(input())
        
        if amount > oranges:
          print(" ")
          print(" ")
          print("you can't sell that much")

        else:
          money=money+(amount*150)
          oranges=oranges-amount
          sold=sold+amount
          bought=bought-sold
          print(" ")
          print(" ")
          print("you have sold " + str(amount) + " oranges")
          print("you have €" + str(money))

      if choise == "4":
        print(" ")
        print(" ")
        print("how many do you want to sell?")
        print("max amout you can sell: " + str(grapes))
        amount=int(input())
        
        if amount > grapes:
          print(" ")
          print(" ")
          print("you can't sell that much")

        else:
          money=money+(amount*600)
          grapes=grapes-amount
          sold=sold+amount
          bought=bought-sold
          print(" ")
          print(" ")
          print("you have sold " + str(amount) + " oranges")
          print("you have €" + str(money))

  if choise == "3":
    #in progress#
    #don't forget to make#
    print(" ")

if opdracht == "2":
  money=int(100)
  rep=int(0)
  bought=int(0)
  sold=int(0)
  apples=int(0)
  bananas=int(0)
  oranges=int(0)
  grapes=int(0)
  
  print(" ")
  print(" ")
  print(" ")
  print("current stats:")
  print("  money: €" + str(money))
  print("  reputation: " + str(rep))
  print("  products bought: " + str(bought))
  print("  products sold: " + str(sold))

  print(" ")
  print(" ")
  print(" ")
  print("choose your next step:")
  print("  1. buy a product")
  print("  2. sell a product")
  print("  3. buy an add-board")
  choise=input()

  if choise == "1":
    print(" ")
    print(" ")
    print(" ")
    print("you have €" + str(money) + " to buy something")
    print("wich of these product do you want to buy?")
    print(" ")
    print("  1. apples:")
    print("    buy price: €1")
    print("    sell price: €2")
    print(" ")
    print("  2. bananas:")
    print("    buy price: €50")
    print("    sell price: €60")
    print(" ")
    print("  3. oranges:")
    print("    buy price: €100")
    print("    sell price: €150")
    print(" ")
    print("  4. grapes:")
    print("    buy price: €500")
    print("    sell price: €600")
    print(" ")

    product=input()

    if product == "1":
      print(" ")
      print(" ")
      print("how many do you want to buy?")
      print("max amout you can afford: " + str(money))
      amount=int(input())

      if amount > money:
        print(" ")
        print(" ")
        print("you can't afford that much")

      else:
        money=money-(amount*1)
        apples=apples+amount
        bought=bought+amount
        print(" ")
        print(" ")
        print("you have bought " + str(amount) + " apples")
        print("you have €" + str(money) + " left")

    if product == "2":
      print(" ")
      print(" ")
      print("how many do you want to buy?")
      print("max amout you can afford: " + str(money/50))
      amount=int(input())

      if amount > money:
        print(" ")
        print(" ")
        print("you can't afford that much")

      else:
        money=money-(amount*50)
        bananas=bananas+amount
        bought=bought+amount
        print(" ")
        print(" ")
        print("you have bought " + str(amount) + " bananas")
        print("you have €" + str(money) + " left")

    if product == "3":
      print(" ")
      print(" ")
      print("how many do you want to buy?")
      print("max amout you can afford: " + str(money/100))
      amount=int(input())

      if amount > money:
        print(" ")
        print(" ")
        print("you can't afford that much")

      else:
        money=money-(amount*100)
        oranges=oranges+amount
        bought=bought+amount
        print(" ")
        print(" ")
        print("you have bought " + str(amount) + " oranges")
        print("you have €" + str(money) + " left")

    if product == "4":
      print(" ")
      print(" ")
      print("how many do you want to buy?")
      print("max amout you can afford: " + str(money/500))
      amount=int(input())

      if amount > money:
        print(" ")
        print(" ")
        print("you can't afford that much")

      else:
        money=money-(amount*500)
        grapes=grapes+amount
        bought=bought+amount
        print(" ")
        print(" ")
        print("you have bought " + str(amount) + " grapes")
        print("you have €" + str(money) + " left")


  if choise == "2":
    print(" ")
    print(" ")
    print(" ")

    if bought == 0:
      print("you haven't bought anything yet")

    else:
      print("you have €" + str(money) + " to sell something")
      print("wich of these product do you want to sell?")
      print(" ")
      print("  1. apples:")
      print("    sell price: €2")
      print(" ")
      print("  2. bananas:")
      print("    sell price: €60")
      print(" ")
      print("  3. oranges:")
      print("    sell price: €150")
      print(" ")
      print("  4. grapes:")
      print("    sell price: €600")
      print(" ")
      choise=input()

      if choise == "1":
        print(" ")
        print(" ")
        print("how many do you want to sell?")
        print("max amout you can sell: " + str(apples))
        amount=int(input())

        if amount > apples:
          print(" ")
          print(" ")
          print("you can't sell that much")

        else:
          money=money+(amount*2)
          apples=apples-amount
          sold=sold+amount
          bought=bought-sold
          print(" ")
          print(" ")
          print("you have sold " + str(amount) + " apples")
          print("you have €" + str(money))

      if choise == "2":
        print(" ")
        print(" ")
        print("how many do you want to sell?")
        print("max amout you can sell: " + str(bananas))
        amount=int(input())

        if amount > bananas:
          print(" ")
          print(" ")
          print("you can't sell that much")

        else:
          money=money+(amount*60)
          bananas=bananas-amount
          sold=sold+amount
          bought=bought-sold
          print(" ")
          print(" ")
          print("you have sold " + str(amount) + " bananas")
          print("you have €" + str(money))

      if choise == "3":
        print(" ")
        print(" ")
        print("how many do you want to sell?")
        print("max amout you can sell: " + str(oranges))
        amount=int(input())

        if amount > oranges:
          print(" ")
          print(" ")
          print("you can't sell that much")

        else:
          money=money+(amount*150)
          oranges=oranges-amount
          sold=sold+amount
          bought=bought-sold
          print(" ")
          print(" ")
          print("you have sold " + str(amount) + " oranges")
          print("you have €" + str(money))

      if choise == "4":
        print(" ")
        print(" ")
        print("how many do you want to sell?")
        print("max amout you can sell: " + str(grapes))
        amount=int(input())

        if amount > grapes:
          print(" ")
          print(" ")
          print("you can't sell that much")

        else:
          money=money+(amount*600)
          grapes=grapes-amount
          sold=sold+amount
          bought=bought-sold
          print(" ")
          print(" ")
          print("you have sold " + str(amount) + " oranges")
          print("you have €" + str(money))

  if choise == "3":
    #in progress#
    #don't forget to make#
    print(" ")

If you’re in the Repl editor, click your Repl’s title, then click the blue cover page button. The URL at that page is the one you can post here.

It should look like this:
https://replit.com/@<your username>/<your repl name>?v=1
Note that your Repl name will have spaces as hyphens (-) and other non alphanumeric characters removed.
This is the link you want: https://replit.com/@cuypersmathis/game?v=1

:smile:

1 Like

You can save the data in a file.

with open("stufftosave.txt", "w") as file: # 'w' stands for write
   file.write("hello, world")

You can get the data by running this code:

with open("stufftosave.txt", "r") as file: # 'r' stands for read
   print(file.read())
2 Likes

can you move all your variables outside the ifs then? Literally just like

becomes

money=int(100)
rep=int(0)
bought=int(0)
sold=int(0)
apples=int(0)
bananas=int(0)
oranges=int(0)
grapes=int(0)
if opdracht == "1":
  ...

Question:
Is it possible to use a variable that is defined inside a “if” command?

Current behavior:
When i press boot and than 2, it says that there are some undifined variables.

Desired behavior
I want to use the variables outside the “if” command to make a game, but i don’t want the variables to reset.

Repl link:
https://replit.com/@cuypersmathis/game#main.py

print("1. start new game")
print("2. load prevorious game")
print("3. wat's new?")
print(" ")
opdracht=input()

import time
time.sleep(1)

if opdracht == "1":
  money=int(100)
  rep=int(0)
  bought=int(0)
  sold=int(0)
  apples=int(0)
  bananas=int(0)
  oranges=int(0)
  grapes=int(0)
  
  print(" ")
  print(" ")
  print(" ")
  print("current stats:")
  print("  money: €" + str(money))
  print("  reputation: " + str(rep))
  print("  products bought: " + str(bought))
  print("  products sold: " + str(sold))

  print(" ")
  print(" ")
  print(" ")
  print("choose your next step:")
  print("  1. buy a product")
  print("  2. sell a product")
  print("  3. buy an add-board")
  choise=input()

  if choise == "1":
    print(" ")
    print(" ")
    print(" ")
    print("you have €" + str(money) + " to buy something")
    print("wich of these product do you want to buy?")
    print(" ")
    print("  1. apples:")
    print("    buy price: €1")
    print("    sell price: €2")
    print(" ")
    print("  2. bananas:")
    print("    buy price: €50")
    print("    sell price: €60")
    print(" ")
    print("  3. oranges:")
    print("    buy price: €100")
    print("    sell price: €150")
    print(" ")
    print("  4. grapes:")
    print("    buy price: €500")
    print("    sell price: €600")
    print(" ")

    product=input()

    if product == "1":
      print(" ")
      print(" ")
      print("how many do you want to buy?")
      print("max amout you can afford: " + str(money))
      amount=int(input())
      
      if amount > money:
        print(" ")
        print(" ")
        print("you can't afford that much")
        
      else:
        money=money-(amount*1)
        apples=apples+amount
        bought=bought+amount
        print(" ")
        print(" ")
        print("you have bought " + str(amount) + " apples")
        print("you have €" + str(money) + " left")

    if product == "2":
      print(" ")
      print(" ")
      print("how many do you want to buy?")
      print("max amout you can afford: " + str(money/50))
      amount=int(input())
      
      if amount > money:
        print(" ")
        print(" ")
        print("you can't afford that much")
        
      else:
        money=money-(amount*50)
        bananas=bananas+amount
        bought=bought+amount
        print(" ")
        print(" ")
        print("you have bought " + str(amount) + " bananas")
        print("you have €" + str(money) + " left")

    if product == "3":
      print(" ")
      print(" ")
      print("how many do you want to buy?")
      print("max amout you can afford: " + str(money/100))
      amount=int(input())
      
      if amount > money:
        print(" ")
        print(" ")
        print("you can't afford that much")
        
      else:
        money=money-(amount*100)
        oranges=oranges+amount
        bought=bought+amount
        print(" ")
        print(" ")
        print("you have bought " + str(amount) + " oranges")
        print("you have €" + str(money) + " left")

    if product == "4":
      print(" ")
      print(" ")
      print("how many do you want to buy?")
      print("max amout you can afford: " + str(money/500))
      amount=int(input())
      
      if amount > money:
        print(" ")
        print(" ")
        print("you can't afford that much")
        
      else:
        money=money-(amount*500)
        grapes=grapes+amount
        bought=bought+amount
        print(" ")
        print(" ")
        print("you have bought " + str(amount) + " grapes")
        print("you have €" + str(money) + " left")


  if choise == "2":
    print(" ")
    print(" ")
    print(" ")
    
    if bought == 0:
      print("you haven't bought anything yet")
      
    else:
      print("you have €" + str(money) + " to sell something")
      print("wich of these product do you want to sell?")
      print(" ")
      print("  1. apples:")
      print("    sell price: €2")
      print(" ")
      print("  2. bananas:")
      print("    sell price: €60")
      print(" ")
      print("  3. oranges:")
      print("    sell price: €150")
      print(" ")
      print("  4. grapes:")
      print("    sell price: €600")
      print(" ")
      choise=input()

      if choise == "1":
        print(" ")
        print(" ")
        print("how many do you want to sell?")
        print("max amout you can sell: " + str(apples))
        amount=int(input())
        
        if amount > apples:
          print(" ")
          print(" ")
          print("you can't sell that much")
          
        else:
          money=money+(amount*2)
          apples=apples-amount
          sold=sold+amount
          bought=bought-sold
          print(" ")
          print(" ")
          print("you have sold " + str(amount) + " apples")
          print("you have €" + str(money))

      if choise == "2":
        print(" ")
        print(" ")
        print("how many do you want to sell?")
        print("max amout you can sell: " + str(bananas))
        amount=int(input())
        
        if amount > bananas:
          print(" ")
          print(" ")
          print("you can't sell that much")

        else:
          money=money+(amount*60)
          bananas=bananas-amount
          sold=sold+amount
          bought=bought-sold
          print(" ")
          print(" ")
          print("you have sold " + str(amount) + " bananas")
          print("you have €" + str(money))

      if choise == "3":
        print(" ")
        print(" ")
        print("how many do you want to sell?")
        print("max amout you can sell: " + str(oranges))
        amount=int(input())
        
        if amount > oranges:
          print(" ")
          print(" ")
          print("you can't sell that much")

        else:
          money=money+(amount*150)
          oranges=oranges-amount
          sold=sold+amount
          bought=bought-sold
          print(" ")
          print(" ")
          print("you have sold " + str(amount) + " oranges")
          print("you have €" + str(money))

      if choise == "4":
        print(" ")
        print(" ")
        print("how many do you want to sell?")
        print("max amout you can sell: " + str(grapes))
        amount=int(input())
        
        if amount > grapes:
          print(" ")
          print(" ")
          print("you can't sell that much")

        else:
          money=money+(amount*600)
          grapes=grapes-amount
          sold=sold+amount
          bought=bought-sold
          print(" ")
          print(" ")
          print("you have sold " + str(amount) + " oranges")
          print("you have €" + str(money))

  if choise == "3":
    #in progress#
    #don't forget to make#
    print(" ")

if opdracht == "2":
  
  print(" ")
  print(" ")
  print(" ")
  print("current stats:")
  print("  money: €" + str(money))
  print("  reputation: " + str(rep))
  print("  products bought: " + str(bought))
  print("  products sold: " + str(sold))

  print(" ")
  print(" ")
  print(" ")
  print("choose your next step:")
  print("  1. buy a product")
  print("  2. sell a product")
  print("  3. buy an add-board")
  choise=input()

  if choise == "1":
    print(" ")
    print(" ")
    print(" ")
    print("you have €" + str(money) + " to buy something")
    print("wich of these product do you want to buy?")
    print(" ")
    print("  1. apples:")
    print("    buy price: €1")
    print("    sell price: €2")
    print(" ")
    print("  2. bananas:")
    print("    buy price: €50")
    print("    sell price: €60")
    print(" ")
    print("  3. oranges:")
    print("    buy price: €100")
    print("    sell price: €150")
    print(" ")
    print("  4. grapes:")
    print("    buy price: €500")
    print("    sell price: €600")
    print(" ")

    product=input()

    if product == "1":
      print(" ")
      print(" ")
      print("how many do you want to buy?")
      print("max amout you can afford: " + str(money))
      amount=int(input())

      if amount > money:
        print(" ")
        print(" ")
        print("you can't afford that much")

      else:
        money=money-(amount*1)
        apples=apples+amount
        bought=bought+amount
        print(" ")
        print(" ")
        print("you have bought " + str(amount) + " apples")
        print("you have €" + str(money) + " left")

    if product == "2":
      print(" ")
      print(" ")
      print("how many do you want to buy?")
      print("max amout you can afford: " + str(money/50))
      amount=int(input())

      if amount > money:
        print(" ")
        print(" ")
        print("you can't afford that much")

      else:
        money=money-(amount*50)
        bananas=bananas+amount
        bought=bought+amount
        print(" ")
        print(" ")
        print("you have bought " + str(amount) + " bananas")
        print("you have €" + str(money) + " left")

    if product == "3":
      print(" ")
      print(" ")
      print("how many do you want to buy?")
      print("max amout you can afford: " + str(money/100))
      amount=int(input())

      if amount > money:
        print(" ")
        print(" ")
        print("you can't afford that much")

      else:
        money=money-(amount*100)
        oranges=oranges+amount
        bought=bought+amount
        print(" ")
        print(" ")
        print("you have bought " + str(amount) + " oranges")
        print("you have €" + str(money) + " left")

    if product == "4":
      print(" ")
      print(" ")
      print("how many do you want to buy?")
      print("max amout you can afford: " + str(money/500))
      amount=int(input())

      if amount > money:
        print(" ")
        print(" ")
        print("you can't afford that much")

      else:
        money=money-(amount*500)
        grapes=grapes+amount
        bought=bought+amount
        print(" ")
        print(" ")
        print("you have bought " + str(amount) + " grapes")
        print("you have €" + str(money) + " left")


  if choise == "2":
    print(" ")
    print(" ")
    print(" ")

    if bought == 0:
      print("you haven't bought anything yet")

    else:
      print("you have €" + str(money) + " to sell something")
      print("wich of these product do you want to sell?")
      print(" ")
      print("  1. apples:")
      print("    sell price: €2")
      print(" ")
      print("  2. bananas:")
      print("    sell price: €60")
      print(" ")
      print("  3. oranges:")
      print("    sell price: €150")
      print(" ")
      print("  4. grapes:")
      print("    sell price: €600")
      print(" ")
      choise=input()

      if choise == "1":
        print(" ")
        print(" ")
        print("how many do you want to sell?")
        print("max amout you can sell: " + str(apples))
        amount=int(input())

        if amount > apples:
          print(" ")
          print(" ")
          print("you can't sell that much")

        else:
          money=money+(amount*2)
          apples=apples-amount
          sold=sold+amount
          bought=bought-sold
          print(" ")
          print(" ")
          print("you have sold " + str(amount) + " apples")
          print("you have €" + str(money))

      if choise == "2":
        print(" ")
        print(" ")
        print("how many do you want to sell?")
        print("max amout you can sell: " + str(bananas))
        amount=int(input())

        if amount > bananas:
          print(" ")
          print(" ")
          print("you can't sell that much")

        else:
          money=money+(amount*60)
          bananas=bananas-amount
          sold=sold+amount
          bought=bought-sold
          print(" ")
          print(" ")
          print("you have sold " + str(amount) + " bananas")
          print("you have €" + str(money))

      if choise == "3":
        print(" ")
        print(" ")
        print("how many do you want to sell?")
        print("max amout you can sell: " + str(oranges))
        amount=int(input())

        if amount > oranges:
          print(" ")
          print(" ")
          print("you can't sell that much")

        else:
          money=money+(amount*150)
          oranges=oranges-amount
          sold=sold+amount
          bought=bought-sold
          print(" ")
          print(" ")
          print("you have sold " + str(amount) + " oranges")
          print("you have €" + str(money))

      if choise == "4":
        print(" ")
        print(" ")
        print("how many do you want to sell?")
        print("max amout you can sell: " + str(grapes))
        amount=int(input())

        if amount > grapes:
          print(" ")
          print(" ")
          print("you can't sell that much")

        else:
          money=money+(amount*600)
          grapes=grapes-amount
          sold=sold+amount
          bought=bought-sold
          print(" ")
          print(" ")
          print("you have sold " + str(amount) + " oranges")
          print("you have €" + str(money))

  if choise == "3":
    #in progress#
    #don't forget to make#
    print(" ")

I con’t change the date, if you do that, you can’t resume with your old stats.

You can just move the definitions outside the statement

To

print("1. start new game")
print("2. load prevorious game")
print("3. wat's new?")
print(" ")
opdracht=input()
money=100
rep=0
bought=0
sold=0
apples=0
bananas=0
oranges=0
grapes=0

import time
time.sleep(1)

if opdracht == "1":
  print(" ")
  print(" ")
  print(" ")
  #code continues
1 Like

how do i save multiple data’s? like the things you have bought also need to be able to be sold. and i have no idea on how i need to add this into the script.

Well, you can just store the data in a format

In his example

The file stuff to save.txt stored hello, world
So you can just store something else, like

with open("stufftosave.txt", "w") as file:
   file.write("Hello Guys")

Which stored Hello Guys instead of Hello, World

Wanna save multiple data? Store them in format!

For example, the save file is save.txt

Apple = "Am Apple"
Banana = "B"
Number1 = 1
AmTrue = True
with open("save.txt", "w") as file:
   separator = "\n===\n" #newline + === + newline
   file.write(str(Apple) + seperator + str(Banana) + seperator + str(Number1) + seperator + str(AmTrue))

In the file, it stored

Am Apple
===
B
===
1
===
AmTrue

Now to get it back (note that everything became string, make it to other type if needed, like in example)

with open("save.txt", "w") as file:
   separator = "\n===\n" #newline + === + newline
   data = file.read().split(separator) #read the file and separate it with the separator used to store it
   #Data: ["Am Apple", "B", "1", "True"]
   Apple = data[0]
   Banana = data[1]
   Number1 = int(data[2])
   AmTrue = bool(data[3])

Now your data are back.

The file writing line in storing code is very long and repeats a lot, which we do not recommend as it is painful to both read, edit, and run
So, here’s a more advance version of storing data (Same save file):

Apple = "Am Apple"
Banana = "B"
Number1 = 1
AmTrue = True

ValueToStore = [Apple, Banana, Number1, AmTrue]

with open("save.txt", "w") as file:
   separator = "\n===\n" #newline + === + newline
   VarToStore = map(str, VarToStore) #make all item string to prevent error
   file.write(separator.join(VarToStore))

thanks dude, it finaly works. do you now how i can put something in a loop easiely?

What do you mean by that?
Can you give me an example?

It is a good programming practice to declare variables always in the scope of their use.
This means that if you want to use them outside if or loop blocks, you should declare them outside of them.
The problem in python is this is not a rule, while in many languages it is and scope is checked. This creates funny things.
For example you declare them inside the if block and sometimes it works sometimes not … because it depends on when the if block is executed :slight_smile:

1 Like

that when you press “2. load prev game” that you are in a loop. that you don’t need to press “2. load prev game”.

In replit, the default code intelligence will warn “x is possibly unbound” for these kinds of things. Very useful.

2 Likes