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
#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


Messages In This Thread
RE: Need help in a Choose Your Own Adventure Program - by ichabod801 - Jan-29-2017, 08:34 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Which method name would you choose? Gribouillis 7 304 May-30-2024, 07:05 AM
Last Post: buran
  Mac os choose file name howard687 1 1,943 Jan-05-2022, 06:54 AM
Last Post: Larz60+
  Loop with choose Irv1n 5 3,353 Sep-16-2021, 09:31 PM
Last Post: deanhystad
  loop adventure game ilikedofs 1 1,779 May-26-2021, 12:43 AM
Last Post: bowlofred
  Choose an element from multidimensional array quest_ 2 2,716 Nov-25-2020, 12:59 AM
Last Post: quest_
  Choose your own adventure game noahc2004 2 2,664 Jun-26-2020, 02:06 PM
Last Post: DPaul
  Please help a newbie choose which programming language to learn. yeto 2 3,593 Feb-25-2019, 12:56 AM
Last Post: yeto
  Waiting in a text adventure StickyLizard 1 50,679 Jan-19-2019, 10:45 PM
Last Post: ichabod801
  User Input to Choose from Dictionary anelliaf 9 26,065 Mar-27-2018, 02:22 PM
Last Post: anelliaf
  Reasons to choose Python over C++? RandoomDude 62 46,763 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