Python Forum
Need a little more help in a Choose Your Own Adventure Program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need a little more help in a Choose Your Own Adventure Program
#1
Keep in my mind before you read this I just started coding and using python in the past week.
Ok so I decided I wanted to create a choose your own adventure in python.  I am almost finished with this but I ran into one problem at the end.  If you look through the source code you will see that you have to choose between 4 or 5 different things.  I want to make the program print something different based on what combination of paths you took, but I have no idea how to do this.  Thanks in advance and please help- Goldberg791

name=input('Enter your name: ')
print("Hello",name)
answer_1=input('Would you like to play choose your own adventure?: ')
if answer_1=='yes':
   print('Okay lets get started')
   answer_2=input('Your name is derik, and you live on planet Xeno.  You are hoping to save up for the new Toto2000, the best and newest spaceship there is, but you know there is only one way to get that spaceship because it is too expensive.'
                  'You either have to rob and steal from the bank of Xeno or work for the rest of your life and try to afford the spaceship at the end of your life.  Type 1 for to get start planning to rob the bank or enter 2 to work for the rest of your life.')
if answer_2=='1':
   print('Now you need to start planning for your hiest at Xeno bank.')
elif answer_2=='2':
   print('Thats lame, Levi')
elif answer_1=='no':
   print('Okay thank you for your time.')
print( 'First you must get a crew for your hiest, you need five people to help you in your hiest.  One person to drive your getaway hover vechile, one person that can pick locks very well, one person that has experience with the high powered drill, and you and one other person will be making sure that no one walks into or by the bank by pretending to be police officers.')
print('There are two options for every job and you must decide who you want, but remember the better person that you choose the less money you will get.')
# The next line is going to be your descion on what driver you want
answer_3=input('Your first decsion is to decide who you want to be driving your getaway hover vehicle.  You can either choose Chris which is the better person at driving or you can choose Harry which is worse.  If you choose Chris is pay cut will be 20% but if you choose Harry his pay cut will be 10%.  Type chris or harry')
if answer_3=="chris":
   print("You chose Chris, your next descion is going to be who you will choose as your locksmith")
elif answer_3=='harry':
   print("You chose Harry, your next descion is going to be who you will choose as your locksmith")
#The next line will be who you choose for the locksmith.
answer_4=input('You can either choose ben who is better but has a 20% cut or you can choose septimus who is worse but has a 10% cut enter ben or setpimus')
if answer_4=='ben':
   print('You chose ben next you will be choosing who will be operating the drill.')
elif answer_4=='septimus':
   print('You chose ben next you will be choosing who will be operating the drill.')
# Next you will be choosing who operates the drill
answer_5=input("You can either choose Amelia who is better but 20% cut or Jenna who has a 10% cut.  Type jenna or amelia.")
if answer_5=='jenna':
   print("You chose Jenna, next you will need to choose who you want to be a police office with you.")
if answer_5=='amelia':
   print("You chose Amelia, next you will need to choose who you want to be a police officer with you.")
#Next you will be choosing who is the other police officer
answer_6=input("You can either choose beetle who is better but has a 20% cut or you can choose Marsha who has a 10% cut.  Type beetle or marsha.")
if answer_6=='beetle':
   print("You chose beetle, you are ready to do the hiest.")
elif answer_6=='marsha':
   print("You chose Marsha you are ready to do the hiest.")
Reply
#2
First, I would suggest checking out this tutorial. Doing a text adventure is a common thing new Python programmers do, but the if/elif/else structure you are using quickly becomes unwieldy. You can use the same sort of techniques shown in that tutorial for choosing the members of the gang, and simplify the code you are using quite a bit. Note that you are repeating the same pattern over and over again: "For <this role>, do you want <person 1> who is good but wants a 20% cut, or <person 2> who is not so good, but wants a 10% cut." Whenever you are repeating the same pattern over and over again, you should think about a loop. That's what loops do, they repeat the same thing. To do that, you need to take the variations, and put them in some sort of data structure. For example:

robbers = [('driver', 'good', 20, 'Chris'), ('driver', 'not so good', 10, 'Harry'), ('locksmith', 'good', 20, 'Ben'), ('locksmith', 'not so good', 10, 'Septimus'), ...]
gang = []
while robbers:
    good = robbers.pop(0)
    bad = robbers.pop(0)
    choice = input('For your {}, do you want {} who is good and wants a 20% cut, or {} who is not so good and wants a 10% cut.'.format(good[0], good[3], bad[3])
    if choice == good[3]:
        gang.append(good)
    elif choice == bad[3]:
        gang.append(bad)
Maybe not the best solution, but it illustrates the technique I'm talking about. And once you have the gang list, it's relatively easy to do things like sum up the total cut the gang wants, print the names of the gang members, find out if the driver is good or not so good, and so on.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
You'll never get anywhere with such a  code structure. The secret is that you should have data that represent the game, typically, a bunch of "states" and then have some simple and generic code (often called the "engine") which, given the current state, can ask questions, make a few decisions, and then set the next state (which can depend on the answers). So, most of your game is described by data, with relatively little code.

Also, when you have something like this:
if answer_6=='beetle':
  print("You chose beetle, you are ready to do the hiest.")
elif answer_6=='marsha':
  print("You chose Marsha you are ready to do the hiest.")
Don(t you think it would be simpler if it were coded:
if answer_6 in ['beetle','marsha']:
  print("You chose {}, you are ready to do the hiest.".format(answer_6))
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#4
Thanks to all who responded, I took Ofnuts advice and fixed the code, but I still don't understand how I to make the program print something different based on what combination of paths you took.
Thanks in advance- Goldberg291
Reply
#5
Your question isn't very clear. Give us an example of what you would want to print given a particular combination of choices.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
For example if somebody chose Harry, ben, amelia, and beetle, it would print out print("Harry has failed his job but ben and amelia completed their job very well, but beetle also failed his job"). Then it could print something like print("You have succeeded the heist") or print("You have failed the heist"). I could also possibly include some kind of random chance for a certain character to succeed or fail.
Thanks, Goldberg291
Reply
#7
Well, using the way I coded it:

success, failure = [], []
for member in gang:
    chance = member[2] * 4
    if random.randrange(100) < chance:
        success.append(member)
    else:
        failure.append(member)
print('The following team members succeeded: {}'.format(', '.join([member[3] for member in success])))
print('The following team members failed: {}'.format(', '.join([member[3] for member in failure])))
if len(success) > len(failure):
    print('The heist was a success')
else:
    print('The heist was a failure')
Using dictionaries or named tuples rather than the plain tuples i used would make this code clearer. Then member[2] could be member['cut'] or member.cut and member[3] could be member['name'] or member.name.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
Keep in mind before you read any of this, I am a beginner and do not know much about coding or python.
So if you run this it will run fine but I wanted to add something.  The thing I want to add at the end is the total amount of money that you get from the bank and if you have succeeded in getting the new rocket ship for 50,000.  Thank you to everyone very much! PS: Below is the source code
-Goldberg291


import random
import sys

name = input('Enter your name: ')
print("Hello",name)
answer_1=input('Would you like to play choose your own adventure?: ')
if answer_1=='yes':
  print('Okay lets get started')
elif answer_1 == 'no':
  print("Ugh")
  sys.exit()
answer_2 = input('\nYour name is {} and you live on planet Xeno.  You are hoping to save up for the new Toto2000, the best and newest spaceship there is, but you know there is only one way to get that spaceship because it is too expensive.\n  \nYou either have to rob and steal from the bank of Xeno or work for the rest of your life and try to afford the spaceship at the end of your life.\n  \nType 1 to start planning to rob the bank or enter 2 to work for the rest of your life.\n'.format(name))
if answer_2=='1':
  print('Now you need to start planning for your hiest at Xeno bank.')
elif answer_2=='2':
  print('Thats lame', name)
  sys.exit()
elif answer_1=='no':
  print('\nOkay thank you for your time.\n')
print( '\nFirst you must get a crew for your hiest, you need five people to help you in your hiest.\n  \nOne person to drive your getaway hover vechile, one person that can pick locks very well,\n \nOne person that has experience with the high powered drill, and you and one other person will be making sure that no one walks into or by the bank by pretending to be police officers.\n')
print('\nThere are two options for every job and you must decide who you want, but remember the better person that you choose the less money you will get.\nThe Toto200 costs $50,000.\nThe bank holds $250,000.\nIf a task goes wrong you and your gang wont be able to steal as much money. ')
# The next line is going to be your descion on what driver you want
answer_3=input('Your first decsion is to decide who you want to be driving your getaway hover vehicle.  You can either choose Chris which is the better person at driving or you can choose Harry which is worse.  If you choose Chris is pay cut will be 20% but if you choose Harry his pay cut will be 10%.  Type chris or harry')
if answer_3 in ['harry', 'chris']:
  print("You chose {}, your next descion is going to be who you will choose as your locksmith".format(answer_3))
# The next line will be who you choose for the locksmith.
answer_4=input('You can either choose ben who is better but has a 20% cut or you can choose septimus who is worse but has a 10% cut enter ben or setpimus')
if answer_4 in ['ben', 'septimus']:
  print('You chose {}, next you will be choosing who will be operating the drill.'.format(answer_4))
# Next you will be choosing who operates the drill
answer_5=input("You can either choose Amelia who is better but 20% cut or Jenna who has a 10% cut.  Type jenna or amelia.")
if answer_5 in ['amelia','jenna']:
  print("You chose {}, next you will need to choose who you want to be a police officer with you.".format(answer_5))
# Next you will be choosing who is the other police officer
answer_6=input("You can either choose beetle who is better but has a 20% cut or you can choose Marsha who has a 10% cut.  Type beetle or marsha.")
if answer_6 in ['beetle','marsha']:
print("You chose {}, you are ready to do the hiest.".format(answer_6))
# Next will be the results for all 20%
# Keeping civilians away
if answer_6=='beetle':
  if random.randrange(1,101) < 80:
      print('Beetle and you have successfully kept all civilians away from the bank by pretending to be police!')
  else:print('Oh no!  Beetle has failed to keep Civilians away and has called the police, which gives you less time to collect money!  You lose $15,000 of your total amount')
# Locksmith
if answer_4=='ben':
  if random.randrange(1,101)<80:
      print("Ben has succeeded in picking the lock into the bank")
  elif random.randrange(1,101)>80:
      print("Oh no! Ben has failed to pick the lock, it takes him 3 tries to pick the lock, which gives you less time to gather money!")
      if random.randrange(0,100)<50:
                 print("You lose $15,000 of your total amount!")
# Drill
if answer_5=='amelia':
  if random.randrange(1,101)<80:
      print('Amelia has succeeded in drilling the whole into the vault.')
  elif random: print('Oh no!  Amelia has failed drilling into the vault, it takes her 3 tries to get into the vault, which gives you less time to collect money!  \nYou lose $15,000 of your total amount!\n')
  # Driver
if answer_3=='chris':
  if random.randrange(1,101) < 80:
      print('Chris succeeded in driving you away')
  else: print('Chris has failed to drive your team away, your gang must walk and drop some of your load off!  You lose $15,000 of your total amount!')
# Next will be the results for all 10%
if answer_6=="marsha":
  if random.randrange(1,101)>40:
      print('Marsha has failed to keep away civilians, the police have been called, which gives you and your gang less time to collect money.  You lose $30,000 of your total amount!')
  else: print("Marsha has succedeed in keeping away civilians")
if answer_4=='septimus':
  if random.randrange(1,101)<40:
      print('Oh No! Septimus has failed to pick the lock, it takes him an extra 3 times, which gives you and your gang less time to collect money!  You lose $30,000 of your total amount!')
  else: print('Septimus has succeeded in picking the lock into the bank!')
if answer_5=='jenna':
  if random.randrange(1,101)<40:
      print('Oh No! Jenna has failed to drill into the vault, it takes her 3 tries to drill into the vault, which gives you and your gang less time to collect money!  You lose $30,000 of your total amount!')
  else: print('Jenna has succeeded in drilling into the vault!')
if answer_3=='harry':
  if random.randrange(1,101)<40:
      print('Oh No! Harry has failed to drive you and your gang away, you and your gang must drop some of your load and walk!  You lose $30,000 of your total amount!')
  else: print('Harry has succeeded in getting the gang away!')
Edited: please stick to standard forum style and use [python] tags around your code. Thanks.
Reply
#9
(Merged threads since it looks like this is the same topic.)
Reply
#10
You just need to put the maximum amount of the heist in a variable. Then whenever you tell the player that so-and-so messed up and lost you $x off the total, subtract that amount from the variable.

BTW, this part won't work very well:

# Locksmith
if answer_4=='ben':
  if random.randrange(1,101)<80:
      print("Ben has succeeded in picking the lock into the bank")
  elif random.randrange(1,101)>80:
      print("Oh no! Ben has failed to pick the lock, it takes him 3 tries to pick the lock, which gives you less time to gather money!")
      if random.randrange(0,100)<50:
                 print("You lose $15,000 of your total amount!")
Each time you call randrange, it's going to give you a different random number. So the first time it could be 90, and the second time it could 20, and then neither part executes. You should just use an else for the second part.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mac os choose file name howard687 1 1,858 Jan-05-2022, 06:54 AM
Last Post: Larz60+
  Loop with choose Irv1n 5 3,223 Sep-16-2021, 09:31 PM
Last Post: deanhystad
  loop adventure game ilikedofs 1 1,699 May-26-2021, 12:43 AM
Last Post: bowlofred
  Choose an element from multidimensional array quest_ 2 2,618 Nov-25-2020, 12:59 AM
Last Post: quest_
  Need help implmenting if/else or case statements for option to choose file format. samlee916 1 2,003 Jul-22-2020, 06:06 PM
Last Post: Larz60+
  Choose your own adventure game noahc2004 2 2,571 Jun-26-2020, 02:06 PM
Last Post: DPaul
  Please help a newbie choose which programming language to learn. yeto 2 3,490 Feb-25-2019, 12:56 AM
Last Post: yeto
  Waiting in a text adventure StickyLizard 1 40,911 Jan-19-2019, 10:45 PM
Last Post: ichabod801
  User Input to Choose from Dictionary anelliaf 9 25,752 Mar-27-2018, 02:22 PM
Last Post: anelliaf
  Reasons to choose Python over C++? RandoomDude 62 45,197 May-03-2017, 05:29 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020