as a first project, i’ve decided to create a simple turn based combat game
i’m running into trouble because my user inputs aren’t giving the expected results. (see comment around line 35)
if anyone could tell me why this is happening i would be very appreciative.
Code is as follows:
name = input('what is thy name, traveler? ' )
hero_hp = 30
bad_id = random.randint(1,9)
initiative = 0
if bad_id == 1:
bad_name = "rat"
elif bad_id == 2:
bad_name = "pig"
elif bad_id == 3:
bad_name = "goblin"
elif bad_id == 4:
bad_name = "mean cat"
elif bad_id == 5:
bad_name = "hobogoblin"
elif bad_id == 6:
bad_name = "Derek"
elif bad_id == 7:
bad_name = "centaur"
elif bad_id == 8:
bad_name = "stump"
elif bad_id == 9:
bad_name = "lich king"
bad_name = "missingno."
bad_hp = bad_id * 6
### here is the combat brain. i am having trouble getting (initiative == 1) to actually affect global variables (hero_hp) and (bad_hp), and (initiative == 2) returns the same result as (initiative == 1) for some reason.
print("your hp: ", hero_hp)
print("enemy hp: ", bad_hp)
print('what will you do, ', name, '?')
print("1 = attack, 2 = surrender")
initiative = int(input())
if initiative == 1:
print(name, " swings wildly!")
print(bad_name, " lunges toward you!")
return (bad_hp - 5* random.randint(1,6))
return initiative == 0
if initiative == 2:
print("A shameful display!")
return hero_hp == 0
while initiative == 0 and hero_hp > 0:
if hero_hp <= 0:
print("You have been defeated, slain by a ", bad_name, ". F in the chat.")
note: codebyte is giving me an EOFerror on line 2 but it works in my compiler.
ETA: hello again! i’ve fixed the issues with my code and i’m proud to say that the combat generator is working as expected :]
final version is here: https://www.online-python.com/uq69GlWbOC
Suggest use your workspace (and link that) rather than a codebyte. Much easier for us to see all the code and perhaps see it run in an environment that supports more of the features of the language. It would appear that
input() may not be supported in codebyte, which is what gives rise to the raised EOFError.
ok thank you! sorry im literally brand new so not sure of best practices for sharing code.
i did end up solving the problem with the help of a friend. here is the final version:
the main issue is that i was using RETURN in the wrong way,
if i understand correctly, return assigns a value to its respective function, when i was trying to assign values to global variables.
return does not assign, but it does help in the process of giving caller access to the local value.
return as an object with two attributes:
return_address: # next instruction immediately following the caller
value_address: # reference to value
When a function is called, the first attribute is assigned the location of where the call came from plus one offset to the next instruction. When the function is exited, we need to know where to go next.
When the return statement is reached, all the values included have their addresses polled (their
id) and that is what gets handed to the caller.
It is because of this simple notion (return as an object) that multiple calls can feasibly be made and handled (asynchronously) by a single function. Every call has this unique object bound to it.
Be sure to Share your workspace. At present the link is 404. Please adjust and repost the link.
Oops! There may well be an assignment, but it is not
return that does the assigning. The caller does that.
print (id(x)) # 2207386895888
a = 42
print (id(a)) # 2207386895888
b = foo(a)
print (id(b)) # 2207386895888