![]() |
Variable Manipulation - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Variable Manipulation (/thread-29394.html) |
Variable Manipulation - Tbot1000 - Aug-31-2020 I have been working on a game which involves leveling up skills through different tasks. When a person types /m in the console and is in the coal mine, among other things the mining_skill variable is supposed to increase. No errors are returned, however, the variable does not increase, it will alternate from 5 to 3. In the program, you can see the variables by typing /skill I tried condensing the program and setting it up the same way and that works perfectly fine - for some reason this doesn't. Thanks! #imports here import random import time import sys #all the variables! health = 20 coins = 700 #skill levels combat_skill = 0 mining_skill = 0 farming_skill = 0 shop_buy_options = ["Lapis Helmet","Lapis Chestplate","Lapis Leggings","Lapis Boots","Hardened Diamond","Young Dragon Fragment","Diamond Helmet","Diamond Chestplate","Diamond Leggings","Diamond Boots","Speed Talisman"] shop_forge_options = ["Reforged Iron Helmet","Reforged Iron Chesplate","Reforged Iron Leggings","Reforged Iron Boots","Reforged Iron Sword","Diamond Helmet","Diamond Chestplate","Diamond Leggings","Diamond Boots", "Diamond Sword","Hardened Diamond Helmet","Hardened Diamond Chestplate","Hardened Diamond Leggings","Hardened Diamond Boots", "Hardened Diamond Sword","Young Dragon Helmet","Young Dragon Chestplate","Young Dragon Leggings","Young Dragon Boots","Dragon Slayer Sword","Aether Shell"] location = ["base"] two_location = ["Spider's Den","Coal Mine","Wheat Fields"] armor_number = 0 inventory = ["Iron_Sword","Chainmail Helmet","Chaimail Chestplate", "Chainmail Leggings", "Chainmail Boots"] name = input("Enter your username:") #player data player_data_1=open(name, "w") player_data_1.close() input(""" Welcome to Dungeon Run! This is a fighter game all about getting the best armor and weapons to kill the Aether Dragon. There are many things for you to explore! Note: Exact spelling is necessary! """) input("In a starter set, I will give you a full set of chaimail armor and a standard sword.") def console_main(): global console_command #global location global shop_buy_options global shop_forge_options global inventory global coins global combat_skill global mining_skill global farming_skill console_command = input("Enter a command. Type /q for the list of all possible commands.") #routine updates player_data_1=open(name, "w") for element in inventory: player_data_1.write(element) player_data_1.close() # if "Iron Chestplate" in inventory #functions if console_command == "/q": print(""" /q: list all commands /e: list inventory /r: return to base /m: mine(if applicable) /l: list warp opprotunities and warp /s: access the shop (if at the base) /d: drop an item /qu: save and quit /location: list current location /skill: display all skill levels """) if console_command == "/skill": print("Mining Skill: ",mining_skill) print("Farming Skill: ",farming_skill) print("Combat Skill: ",combat_skill) if console_command == "/e": print(inventory) if console_command == "/location": print(location) if console_command == "/r": del location[:] print (location) location.append("base") ("base") print ("You are at ",location) if console_command == "/l": print("Choose which location to warp to.") del location[:] print("Available locations you've unlocked:",two_location,": ") location.append(input("Enter your chocie: ")) print ("You are at ",location) if console_command == "/m": if location == ["Coal Mine"]: mining_skill = mining_skill =+ 5 mining_randomness = random.randint(1,101) if mining_randomness >= 70: inventory.append("Coal") print("You found Coal!") time.sleep(1.5) mining_skill = mining_skill =+ 3 else: print("You found nothing of importance.") time.sleep(.33) if console_command == "/qu": sys.exit() if console_command == "/d": drop_item = input("Drop which item?") inventory.remove(drop_item) print("You dropped one ",drop_item) #shop code if console_command == "/s" and location == ["base"]: print(location) print("Welcome to the shop! Here you can forge things, sell others, and buy yet others!") shop_options = input(""" Buy Sell Forge """) #Shop: Buy if shop_options == "Buy": print("You have ",coins," coins in your account") print("A catalog of all available items to buy (Prices are displayed when you open an item): (type /n to not buy anything") shop_buy = input(shop_buy_options) if shop_buy == "Lapis Helmet": buy_confirm_lapis_helmet = input("Type Y / N to confirm order. Price: 500 coins") if buy_confirm_lapis_helmet == "Y" and coins > 500: inventory.append("Lapis Helmet") coins = coins - 500 print("You bought a Lapis Helmet!") else: return if shop_buy == "Lapis Chestplate" and coins > 800: buy_confirm_lapis_chestplate = input("Type Y / N to confirm order. Price: 800 coins") if buy_confirm_lapis_chestplate == "Y": inventory.append("Lapis Chestplate") coins = coins - 800 print("You bought a Lapis Chestplate!") else: return if shop_buy == "Lapis Leggings" and coins > 700: buy_confirm_lapis_leggings = input("Type Y / N to confirm order. Price: 700 coins") if buy_confirm_lapis_leggings == "Y": inventory.append("Lapis Leggings") coins = coins - 700 print("You bought a pair of Lapis Leggings!") else: return if shop_buy == "Lapis Boots" and coins > 400: buy_confirm_lapis_boots = input("Type Y / N to confirm order. Price: 400 coins") if buy_confirm_lapis_boots == "Y": inventory.append("Lapis Boots") coins = coins - 400 print("You bought a pair of Lapis Boots!") else: return if shop_options == "Forge": print("You have ",coins," coins in your account") print("A catalog of all available items to Forge (It is free to forge an item, but you must have all the required materials):") forge_options = input(shop_forge_options) if forge_options == "Reforged Iron Helmet": print("You need 27 pieces of coal to reforge any iron armor.") confirm_reforge_iron_helmet = input("Reforge Iron Helmet? Y / N") coal_counter_helmet = 0 if confirm_reforge_iron_helmet == "Y" and "Iron Helmet" in inventory: for x in range(0,3): if "Coal" in inventory: inventory.remove("Coal") coal_counter_helmet = coal_counter_helmet =+ 1 else: print("Reforge Failed! Not enough coal!") if coal_counter_helmet == 1: inventory.append("Reforged Iron Helmet") print("Reforge Successful! Reforged Iron Helmet created. Make sure to equip it!") else: print("Reforge cancelled.") if forge_options == "Reforged Iron Chestplate": print("You need 27 pieces of coal to reforge any iron armor.") confirm_reforge_iron_chestplate = input("Reforge Iron Chestplate? Y / N") coal_counter_chestplate = 0 if confirm_reforge_iron_chestplate == "Y" and "Iron Chestplate" in inventory: for x in range(0,28): if "Coal" in inventory: inventory.remove("Coal") coal_counter_chestplate = coal_counter_chestplate =+ 1 else: print("Reforge Failed! Not enough coal!") if coal_counter_chestplate == 1: inventory.append("Reforged Iron Chestplate") print("Reforge Successful! Reforged Iron Chestplate created. Make sure to equip it!") else: print("Reforge cancelled.") if forge_options == "Reforged Iron Leggings": print("You need 27 pieces of coal to reforge any iron armor. Make sure you do!") confirm_reforge_iron_leggings = input("Reforge Iron Leggings? Y / N") coal_counter_leggings = 0 if confirm_reforge_iron_leggings == "Y" and "Iron Leggings" in inventory: for x in range(0,28): if "Coal" in inventory: inventory.remove("Coal") coal_counter_leggings = coal_counter_leggings =+ 1 else: print("Reforge Failed! Not enough coal!") if coal_counter_leggings == 1: inventory.append("Reforged Iron Leggings") print("Reforge Successful! Reforged Iron Leggings created. Make sure to equip it!") else: print("Reforge cancelled.") if forge_options == "Reforged Iron Boots": print("You need 27 pieces of coal to reforge any iron armor.") confirm_reforge_iron_boots = input("Reforge Iron Boots? Y / N") coal_counter_boots = 0 if confirm_reforge_iron_boots == "Y" and "Iron Boots" in inventory: for x in range(0,28): if "Coal" in inventory: inventory.remove("Coal") coal_counter_boots = coal_counter_boots =+ 1 else: print("Reforge Failed! Not enough coal!") if coal_counter_boots == 1: inventory.append("Reforged Iron Boots") print("Reforge Successful! Reforged Iron Boots created. Make sure to equip it!") else: print("Reforge cancelled.") console_main() while 1==1: console_main() RE: Variable Manipulation - buran - Aug-31-2020 check line 106 RE: Variable Manipulation - Tbot1000 - Aug-31-2020 There doesn't seem to be any problem with mining_skill = mining_skill =+ 5... I must be missing something obvious here. RE: Variable Manipulation - buran - Aug-31-2020 it should be either mining_skill = mining_skill + 5or mining_skill += 5at the moment you are just comparing mining_skill and the result of evaluation of mining_skill =+ 5 . My initial thought was it should throw an error, but obliviously it's a valid code - chained assignment>>> spam = eggs = 'foo' >>> spam 'foo' >>> eggs 'foo'in your case spam and eggs are both maining_skills
RE: Variable Manipulation - bowlofred - Aug-31-2020 x += 3is a shorthand to increase a number by a value. You have instead: x = x = +5Theres no incrementing, just setting a variable (twice, sorta) RE: Variable Manipulation - Tbot1000 - Aug-31-2020 Ah. I believe this is the proper syntax for C++, I think I just confused them. Thanks a bunch! RE: Variable Manipulation - buran - Aug-31-2020 (Aug-31-2020, 05:40 PM)buran Wrote: it's a valid code - chained assignment RE: Variable Manipulation - deanhystad - Aug-31-2020 (Aug-31-2020, 05:47 PM)Tbot1000 Wrote: Ah. I believe this is the proper syntax for C++, I think I just confused them. Thanks a bunch!x = x =+ 5 is syntactically valid, but it is nonsensical. It evaluates as x = +5 x = x It is the same in C++ and Python. Will not throw an error, but not correct code unless you just want to set x to the value 5. You may have been thinking of x = x += 5. As with the earlier example in C++ this is syntactically valid and nonsensical. It evaluates as x += 5 x = x And x += 5 evaluates as x = x + 5 This code adds 5 to x, but the extra assignment is unnecessary. In Python x = x += 5 is invalid code. |