Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Why does trinket say that there is something wrong with my program?

0 votes
asked Mar 26, 2019 by Sean
I've been working on a program, and I wanted to work on a part of it on a separate program, so I did. When I hit run, it said that something was wrong with it, but I couldn't find anything wrong with it. The line that had that problem was line 76. As far as I can tell, there is nothing wrong with my entire program. Can you please help? The code for the program is below:

input64 = "0"
strength = 1
perception = 1
endurance = 1
charisma = 1
intelligence = 1
agility = 1
luck = 1
turbo = 0
purifiedwater = 0
nukacola = 0
nukacide = 0
nukacolaquartz = 0
nukacolavictory = 0
nukacolafusion = 0
nukacolaquantum = 0
sunsetsarsparilla = 0
mentats = 0
buffout = 0
dead = False
bat = True
knife = True
questvar3 = True
questvar2 = True
suit = 5
psychobonus = 0
gun = "revolver"
equippedgun = 5
health = 200
rad = 0
revolver = True
ammo357 = 30
laserpistol = True
energycells = 30
pistol9mm = True
ammo9mm = 30
endurance = 3
medxbonus = 0
stimpaks = 0
superstimpaks = 0
radaway = 0
strength = 1
perception = 1
turbo = 0
medx = 0
escape = False
radx = 0
psycho = 0
stealthboy = 0
invisible = False
questvar = True
monsteralive = True
monster = "yao guai"
monsterattack = 30
monstershield = 5
monsterrad = 0
monsterspeed = 5
monsterhealth = 100
monsterattackstyle = "melee"
monsterdamage = monsterattack - suit
print "[You run into a wild " + str(monster) + "! Will you try to run, fight it, or use an item?]"
if questvar4 == True and dead == False and monsteralive == True :
  monsterdamage = monsterattack - suit
  monsterrad = 0
  print ""
  if psychobonus > 0:
    psychobonus -= 1
  if medxbonus > 0:
    medxbonus -= 1
  print "Health - " + str(health)
  print "Radiation - " + str(rad)
  input64 = input("""1 - Fight
2 - Run
3 - Use Chem
4 - Use Food"""
  if input64 == "2":
    if endurance >= monsterspeed:
    3 escape = True
    else:
      print "[You weren't able to run fast enough!]"
      escape = False
  elif input64 == "1":
    if gun == "laserpistol":
      if energycells > 0:
        energycells -= 1
        attack = True
      else:
        print "[You're out of ammo!]"
        if revolver == True and ammo357 > 0:
          equippedgun = 5
          gun = "revolver"
          print "[You switched from the laserpistol to the revolver.]"
          attack = True
          ammo357 -= 1
        elif pistol9mm == True and ammo9mm > 0:
          equippedgun = 2
          gun = "pistol9mm"
          print "[You switched from the laserpistol to the 9mm pistol.]"
          attack = True
          ammo9mm -= 1
    elif gun == "revolver":
      if ammo357 > 0:
        ammo357 -= 1
        attack = True
      else:
        print "[You're out of ammo!]"
        if laserpistol == True and energycells > 0:
          gun = "laserpistol"
          print "[You switched from the revolver to the laser pistol.]"
          equippedgun = 10
          energycells -= 1
          attack = True
        elif pistol9mm == True and ammo9mm > 0:
          gun = "pistol9mm"
          print "[You switched from the revolver to the 9mm pistol.]"
          equippedgun = 2
          ammo9mm -= 1
          attack = True
    elif gun == "pistol9mm":
      if ammo9mm > 0:
        ammo9mm -= 1
        attack = True
      else:
        print "[You're out of ammo!]"
        if laserpistol == True and energycells > 0:
          gun = "laserpistol"
          print "[You switched from the 9mm pistol to the laser pistol.]"
          equippedgun = 10
          energycells -= 1
          attack = True
        elif revolver == True and ammo357 > 0:
          gun = "revolver"
          print "[You switched from the 9mm pistol to the revolver.]"
          equippedgun = 5
          ammo357 -= 1
          attack = True
    elif bat == True:
      melee = "bat"
      equippedmelee = 5
      meleeattack = True
    elif knife == True:
      melee = "knife"
      equippedmelee = 7
      meleeattack = True
  elif input64 == "3":
    input7 = input("""1 - Stimpak (""" + str(stimpaks) + """)
2 - Super Stimpak (""" + str(superstimpaks) + """)
3 - Radaway (""" + str(radaway) + """)
4 - Rad-X (""" + str(radx) + """)
5 - Med-X (""" + str(medx) + """)
6 - Psycho (""" + str(psycho) + """)
7 - Buffout (""" + str(buffout) + """)
8 - Mentats (""" + str(mentats) + """""")
    if input7 == "1":
      if stimpaks > 0:
        print "You used a stimpak."
        stimpaks -= 1
        if health <= 100:
          health += 100
        else:
          health = 200
      else:
        print "You don't have any stimpaks."
    elif input7 == "2":
      if superstimpaks > 0:
        print "You used a super stimpak."
        superstimpak -= 1
        health = 200
      else:
        print "You don't have any super stimpaks."
  elif input64 == "4":
    input7 = input("""1 - Nuka-Cola """ + str(nukacola) + """
2 - Nuka-Cola Quartz """ + str(nukacolaquartz) + """
3 - Nuka-Cola Victory """ + str(nukacolavictory) + """
4 - Nuka-Cola Fusion """ + str(nukacolafusion) + """
5 - Nuka-Cola Quantum """ + str(nukacolaquantum) + """
6 - Nukacide """ + str(nukacide) + """
7 - Sunset Sarsparilla """ + str(sunsetsarsparilla) + """
8 - Purified Water """ + str(purifiedwater))
  if attack == True:
    fullgundamage = perception * equippedgun
    if psychobonus != 0:
      fullgundamage = fullgundamage * 2
    monsterhealth -= gundamage
    print "[You shot the " + monster + ".]"
    attack = False
  elif meleeattack == True:
    fullmeleedamage = (int(strength) * int(equippedmelee)) - int(monstershield)
    if psychobonus != 0:
      fullmeleedamage = fullmeleedamage * 2
    print "[You hit the " + monster + " with your melee weapon.]"
  input65 = input("")
  print ""
  if monsterhealth <= 0:
    monsteralive = False
  if invisible != True and escape == False and monsteralive == True:
    print "[The " + monster + " attacked. it did " + str(monsterdamage) + " damage.]"
    if medxbonus != 0:
      monsterdamage -= 20
      if monsterdamage < 5:
        monsterdamage = 5
    health -= monsterdamage
  if monsterrad > 0:
    print "[It also dealt " + str(monsterrad) + " radiation.]"
    if radresistance == True:
      monsterrad -= 20:
      if monsterrad < 5:
        monsterrad = 5
    rad += monsterrad
  elif invisible == True:
    print ""
  if rad >= 200:
    health -= 20
  elif rad >= 150:
    health -= 15
  elif rad >= 100:
    health -= 10
  elif rad >= 50:
    health -= 5
  if health <= 0:
    dead = True
    questvar3 = False
  input2 = input("")
  if monsteralive == False:
    print "The " + monster + " is dead."

1 Answer

0 votes
answered Mar 28, 2019 by Anonymous Programmer

Which line is 76?  Don't make us count them.
What's the error message?  That usually indicates what the problem is.

But because I was curious what this whole thing was about, I copied it into the onlinegdb Python interpreter (which I had assumed you were using) and gave it a whirl.

Well, you've written Python2 code, and the online interpreter is Python3, so I had to fix all the print "foo" to print("foo") and then it was ready to go.  Mostly.

In any case, it looks like there is no problem with line 76 by itself.  The problem is that you didn't properly end the input command that was started on line 72.  It needs a closing parenthesis, making it:

  input64 = input("""1 - Fight
2 - Run
3 - Use Chem
4 - Use Food""")

Although once you fix that, you'll have a problem with line 78, "3 escape = True".

And then line 206 will need to change the colon ':' to a semicolon ';' (or at least remove it).

And line 62 is trying to access questvar4 which has not been defined here; is it supplied somewhere else?  Or did you mean to use questvar, questvar2, or questvar3 instead?

And line 184 references gundamage, but probably you meant fullgundamage?

It looks like the variables attack and meleeattack are not defined before they are first used, unless the user happens to select "1 - fight" as their action.  (all other choices, including invalid ones, run into the problem)

I assume you would notice and fix all those problems after you got the first one fixed.

On a side note, it is a good idea to have variable names indicate something about what they hold.  So "input64" could be "playerChoice" or "userInput" or something else meaningful.  The name "input64" gives no hint what it's used for, especially when there are red herrings like "input2" and "input65" laying around.  For those two situations, it appears that what you really mean to be doing is something like input("(press Enter to continue)") without assigning the result to a variable (since you never look at it).

You should also learn how to make functions, so that you can divide up the code into manageable pieces, with each function doing one meaningful unit of work.  Right now the entire program is a mass of nested ifs and elses which is really hard to read and maintain.  You could make a chooseAction() function, which prompts the user for what action they would like to take, then calls fight(), run(), useChem(), or useFood() based on the input.  Then it would be much clearer what is happening, and it's easier to add new features like letting the user choose again if they gave bad input (if the useChem() or useFood() return False, because the user has no chem/food, or because they decided to not use them).  And you could have a function which displays Health and Radiation after every action/round, rather than just once at the beginning.

After you've figured out functions, it shouldn't be too hard to pick up Object-Oriented Programming, which will make it easier to collect data into meaningful groups, like weapons, or food, or chem…

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...