February 6th, 2013, 12:02 PM
-
Problem with a simple text game
Hey!
I've a problem that I cannot identify. It's hard to describe the situation, but while playing the game, it randomly stops after the user has entered an input...
The game is in Estonian. Firstly it asks for name, then you have to choose a class.
If you want to step into testing then after inputing your name and class, choose (1) so you start wandering. After some time looping 1 + ENTER, the game decides to stop.
I haven't implemented monsters yet just because the game crashes 
Thanks in advance!
Code:
Code:
import random
def randUitama():
global uitama
uitama = random.randint(1,5)
def uitamine():
global joud, hp, kuld, kaitse, uitama
randUitama()
if uitama == 1:
special = random.randint(1,10)
if special == 1:
print('Söid roiskunud kärbseseene ära, mis taastas kõik su elupunktid!')
print('Uita edasi(1) | Tagasi külla(2)')
valik = input()
if valik == '1':
uitamine()
else:
kyla()
hp = 10
elif special == 2:
spec = random.randint(1,3)
if spec == 2:
print('Jõid allikast võluvett ning said 2 jõudu juurde!')
joud = joud + 2
else:
print('Kõndisid mitu kilomeetrit, kuid midagi ei juhtunud...')
print('Uita edasi(1) | Tagasi külla(2)')
valik = input()
if valik == '1':
uitamine()
else:
kyla()
elif special == 3:
print('Astusid püünisesse ja kaotasid 4 elupunkti!')
hp = hp - 4
print('Uita edasi(1) | Tagasi külla(2)')
valik = input()
if valik == '1':
uitamine()
else:
kyla()
elif special == 4:
kaitsePluss = random.randint(1,3)
if kaitsePluss == 2:
print('Hüppasid langeva puu eest ära ja said 2 kaitsepunkti!')
kaitse = kaitse + 2
print('Uita edasi(1) | Tagasi külla(2)')
valik = input()
if valik == '1':
uitamine()
else:
kyla()
elif special == 5:
randomKuld = random.randint(1,15)
print('Leidsid mahajäetud onnist', randomKuld ,'kulda!')
kuld = kuld + randomKuld
print('Uita edasi(1) | Tagasi külla(2)')
valik = input()
if valik == '1':
uitamine()
else:
kyla()
else:
print('Kõndisid mitu kilomeetrit, kuid midagi ei juhtunud...')
print('Uita edasi(1) | Tagasi külla(2)')
valik = input()
if valik == '1':
uitamine()
else:
kyla()
elif uitama == 2 or uitama == 3:
print('uitamine 2')
print('Uita edasi(1) | Tagasi külla(2)')
valik = input()
if valik == '1':
uitamine()
else:
kyla()
elif uitama == 4 or uitama == 5:
print('Kõndisid mitu kilomeetrit, kuid midagi ei juhtunud...')
print('Uita edasi(1) | Tagasi külla(2)')
valik = input()
if valik == '1':
uitamine()
else:
kyla()
def pood():
global kuld
print('\nHAWKSI pood: Soodsaimad relvad ja turvised kogu GRIMETHORPE\'is!')
if seisus == '1':
print(' NIMI\t\t\tHIND\t NIMI\t\t\t\tHIND')
print('(1)Vasar(jõud+5)\t10\t(a)Kiiver(kaitse+5)\t\t10')
print('(2)Pistoda(jõud+10)\t50\t(b)Kilp(kaitse+10)\t\t50')
print('(3)Mõõk(jõud+25)\t100\t(c)Soomuspüksid(kaitse+25)\t100')
print('(4)Hellebard(jõud+50)\t500\t(d)Raudrüü(kaitse+50)\t\t500')
else:
print(' NIMI\t\t\t\tHIND\t NIMI\t\t\t\tHIND')
print('(1)Sirelileht(võlujõud+5)\t10\t(a)Kübar(kaitse+5)\t\t10')
print('(2)Tammeoks(võlujõud+10)\t50\t(b)Loitsuraamat(kaitse+10)\t50')
print('(3)Vahtrakaigas(võlujõud+25)\t100\t(c)Nahkpüksid(kaitse+25)\t100')
print('(4)Mahagonist sau(võlujõud+50)\t500\t(d)Võlukuub(kaitse+50)\t\t500')
print('\nMida soovite(nr.|täht|lahku(3))? Soovin: ', end=' ')
ost = input()
if seisus == '1' or seisus == '2':
if ost == '1' and kuld >= 10:
joud = 15
kuld = kuld - 10
ostOnnestus = 1
elif ost == '2' and kuld >= 50:
joud = 20
kuld -= 50
ostOnnestus = 1
elif ost == '3' and kuld >= 100:
joud = 35
kuld -= 100
ostOnnestus = 1
elif ost == '4' and kuld >= 500:
joud = 60
kuld -= 500
ostOnnestus = 1
elif ost == 'a' and kuld >= 10:
kaitse = 15
kuld -= 10
ostOnnestus = 1
elif ost == 'b' and kuld >= 50:
kaitse = 20
kuld -= 50
ostOnnestus = 1
elif ost == 'c' and kuld >= 100:
kaitse = 35
kuld -= 100
ostOnnestus = 1
elif ost == 'd' and kuld >= 500:
kaitse = 60
kuld -= 500
ostOnnestus = 1
elif ost == '3':
kyla()
else:
print('Pole piisavalt KULDA!')
ostOnnestus = 0
if ostOnnestus == 1:
print('Ost õnnestus!')
print('Tagasi GRIMETHORPE\'i(1) | Uuesti ostma(2))', end=' ')
poodBoolean = input()
if poodBoolean == '1':
kyla()
else:
pood()
def kyla():
global kuld, joud, hp, kaitse
print('\nTervitused, ' + nimi + ', olete saabunud GRIMETHORPE\'i!')
print('\nKUHU MINNA?\t\tANDMED')
print('Uitama(1)\t\tKuld:\t ', kuld, '\nPood(2)\t\t\tJõud:\t ', joud, '\nPank(3)\t\t\tKaitse:\t ', kaitse, '\n\t\t\tElupunktid: ', hp)
kuhuminna = input()
if kuhuminna == '1':
uitamine()
elif kuhuminna == '2':
pood()
elif kuhuminna == '3':
pank()
else:
kyla()
global kuld, joud, hp, kaitse, seisus, nimi, uitama
kuld = 0
joud = 10
hp = 10
kaitse = 10
print ('Tegelase nimi:', end=' ')
nimi = input()
print('Vali seisus (Sõdalane(1), Nõid(2)):', end =' ')
seisus = input()
kyla()
February 6th, 2013, 02:52 PM
-
It's difficult to analyze this code. Partially due to the language barrier, but mainly, the massive overuse of global variables in every function make it a de-bugging nightmare. Re-factor your functions using proper input arguments and return values and you'll probably spot your problems.
February 6th, 2013, 03:53 PM
-
Sorry for the global variables. I've been looking everywhere for an explanation on how to get get different variables into functions, so I did my best to just make it work 
Anyway. There has to be something wrong with 'input()' thingies. Every time it stops after I input something.
I'll let my brains calm down just a little bit and look it up bit by bit.
Thanks for a suggestion, Marbelous!
February 6th, 2013, 04:45 PM
-
Run your program in python3 .
(input function works differently in python2 and python3 versions)
[code]
Code tags[/code] are essential for python code and Makefiles!
February 7th, 2013, 12:34 PM
-
Originally Posted by b49P23TIvg
Run your program in python3 .
(input function works differently in python2 and python3 versions)
I am using python 3.3.
February 7th, 2013, 12:43 PM
-
OK, I read your message more closely. Obviously you used python 3 or the game would have quit sooner.
I played the game for a while and it crashed with
...
File "p.py", line 171, in kyla
pank()
NameError: global name 'pank' is not defined
What error message do you get?
[code]
Code tags[/code] are essential for python code and Makefiles!
February 7th, 2013, 01:02 PM
-
Originally Posted by b49P23TIvg
OK, I read your message more closely. Obviously you used python 3 or the game would have quit sooner.
I played the game for a while and it crashed with
...
File "p.py", line 171, in kyla
pank()
NameError: global name 'pank' is not defined
What error message do you get?
Sorry for that! 'Pank' stands for 'Bank' and it is not implemented at the moment. The problem occurs if you just input the first 2 caps and continue to 'Uitama(1)'.
Then spam number '1' + ENTER and the game will just stop after a few inputs.
I'm still trying to solve it myself, too.
Thanks for helping!
February 7th, 2013, 10:55 PM
-
When I run your program as
$ yes 1 | python3 p.py
is similar to
Code:
import random
def f():
if random.randint(1,75) < 75:
f()
f()
because it terminates only when---follow the red highlights
Code:
def uitamine():
global joud, hp, kuld, kaitse, uitama
randUitama()
if uitama == 1: # 1 in 5 chance
special = random.randint(1,10)
if special == 1:
print('Söid roiskunud kärbseseene ära, mis taastas kõik su elupunktid!')
print('Uita edasi(1) | Tagasi külla(2)')
valik = input()
if valik == '1':
uitamine()
else:
kyla()
hp = 10
elif special == 2:
spec = random.randint(1,3)
if spec == 2:
print('Jõid allikast võluvett ning said 2 jõudu juurde!')
joud = joud + 2
else:
print('Kõndisid mitu kilomeetrit, kuid midagi ei juhtunud...')
print('Uita edasi(1) | Tagasi külla(2)')
valik = input()
if valik == '1':
uitamine()
else:
kyla()
elif special == 3:
print('Astusid püünisesse ja kaotasid 4 elupunkti!')
hp = hp - 4
print('Uita edasi(1) | Tagasi külla(2)')
valik = input()
if valik == '1':
uitamine()
else:
kyla()
elif special == 4: # 1 in 10 chance
kaitsePluss = random.randint(1,3)
if kaitsePluss == 2:
print('Hüppasid langeva puu eest ära ja said 2 kaitsepunkti!')
kaitse = kaitse + 2
print('Uita edasi(1) | Tagasi külla(2)')
valik = input()
if valik == '1': # 2/3 chance that this does NOT happen
uitamine()
else:
kyla()
Lesson: don't use recursion to implement a menu. If you had written the code as you intended it would have consumed the stack and exited without grace. Messy.
Instead, write kyla like this
Code:
def kyla():
global kuld, joud, hp, kaitse
while True:
print('\nTervitused, ' + nimi + ', olete saabunud GRIMETHORPE\'i!')
print('\nKUHU MINNA?\t\tANDMED')
print('Uitama(1)\t\tKuld:\t ', kuld, '\nPood(2)\t\t\tJõud:\t ', joud, '\nPank(3)\t\t\tKaitse:\t ', kaitse, '\n\t\t\tElupunktid: ', hp)
kuhuminna = input()
if kuhuminna == '1':
uitamine()
elif kuhuminna == '2':
pood()
elif kuhuminna == '3':
pank()
and call kyla once from only the main module. You must also break the recursion in uitamine .
Your program requires major rewrite.
[code]
Code tags[/code] are essential for python code and Makefiles!