Python Forum

Full Version: Beginner Assistance
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

Personal background:
    My first forum post, teaching myself programming, and teaching myself python so please be patient with me.

Program Background:
    I am working on recreating the Oregon Trail in Python from Basic.
    My source for the code is page 135 of creative computing magazine May edition 1978 accessed from archive.org website

Question (stems from around line 100):
    How do I create force the user to input an integer 1-5?

(I am early in the stages of rewriting the program so please offer any suggestions to help a novice)

# This is a python version of the Oregon Trail.
# The code was published in creative computing magazine
# in the May/June issue from 1978.
# The original code appears to be BASIC.

# Programming is done by Mark R. Weems as part of his
# attempt to self learn programming with Python being
# one of the languages he is learning.

## Original Code includes intro...

## Program name - Oregon    Version:01/01/78
## Original programming by Bill Heinemann - 1971
## Support research and materials by Don Ravitsch
##   Minnesota Educational Computing Consortium Staff
## CDC Cyber 70/73-26   BASIC 3.1
## Documentation booklet 'Oregon' available from
##   MECC Support Services
##   2520 Broadway Drive
##   St. Paul, MN 55113

# Meanings of variables used will be noted prior to variable

print ("""
   Do you need instructions?
   1 Yes
   2 No
""")

# C5 was the original code for yes/no responses to questions
# Here I will keep C5 as the variable
C5 = int(input())

while C5 == 1:
   print ("""
   ***Instructions***
   This program simulates a trip over the Oregon Trail from
   Independence, Missouri to Oregon city, Oregon in 1847.
   Your family if five will cover the 2040 mile Oredon Trail
   in 5-6 months --- if you make it alive.

   You had saved $900 to spend for the trip, and you've just
       paid $200 for a wagon.
   You will need to spend the rest of your money on the
       following items:

       Oxen - You can spend $200-$300 on your team
           The more you spend, the faster you will go
           because you will have better animals.

       Food - The more you have, The less chance there
           is of getting sick.

       Ammunition - $1 buys a belt of 50 bullets.
           You will need bullets for attacks by animals
           and bandits, and for hunting food.

       Clothing - This is especially important for the cold
           weather you will encounter when crossing
           the mountains.

       Miscellaneous Supplies - This includes medicine and
           other things you will need for sickness
           and emergency repairs.

   You can spend all your money before you start your trip -
   or you can save some of your cash to spend at Forts along
   the way when you run low. However, items cost more at
   the Forts. You can also go hunting along the way yo get
   more food.
   Whenever you have to use your trusty rifle along the way,
   you will be told to type a word (one that sounds like a
   gun shot). The faster you type the word and hit the
   ***Enter*** key, the better luck you'll have with your gun.

   At each turn, all items are shown in Dollar amounts
   except bullets.
   When asked to enter money amounts, don't use **$**.

   Good Luck!

   """)
# Now to break the loop that is created if the user types 1
   C5 = 0

print ("""
   How good a shot are you with a rifle?
   1. Ace Marksman
   2. Good Shot
   3. Fair Middlin'
   4. Need More Practice
   5. Shaky Knees
   Enter one of the above -- The better you claim you are, the
   faster you'll have to be with your gun to be successful.
   """)

# D9 is the coice of shooting expertise level
D9 = int(input())

# Create something to force a correct input
VC1 = [1, 2, 3, 4, 5]

while D9 != VC1:
   print ("That is not a correct value. Try again.")
   D9 = int(input())
else:
   print ("You selected", D9)
Instead of
while D9 != VC1:
just do
while D9 in VC1:
By the way, the way things are done in Basic (e.g. variable names) might not be good habit to follow. I saw you left a comment about that, but I think it's important enough to mention anyway.
miseydel,

Thank you for the reply. Your technique made the while statement work.

The issue I run into is that when I do that, if they enter a wrong number twice they exit the loop somehow. (see adjusted syntax below)

I also appreciate the input regarding Basic variables. If there is some means to 'clean up' my code I am willing to hear input as well.

Is it best to GitHub and make public for a new user to learn how to write good code?

Thanks again.

while D9 in VC1:
  print ("You selected", D9)
  break
else:
  print ("That is not a correct value. Try again.")
  D9 = int(input())
Sorry, I should have said "not in"

The first code cleanup I would recommend is meaningful variable names. Then, you could use functions or something but this is short enough that you might be better off focusing on your next project than trying to refine this one.

(Jun-07-2017, 07:20 PM)liamwemyss Wrote: [ -> ]Is it best to GitHub and make public for a new user to learn how to write good code?
Github is fantastic, but putting code on Github doesn't mean people will read it. If you make a post here and use code tags, as you did, there's a really high chance someone will take a look at it. The longer it is though, the more likely people will find something easier to do :)
misceydel,

I tried the 'not in', but it did not work for me. Be forewarned I am doing my programs on pythonanywhere.com so that could also be part of my issue.

I will keep plugging away at it and eventually recreate the Oregon Trail. I do appreciate your reviews so far.

Is there some method that people use to plan out a program prior to just sitting down and writing it. Like Boolean flowcharts or something. Is there a resource I can access to teach how to plan a program. I am EXTREMELY grateful there are those here willing to help with syntax, and hopefully one day I will get knowledgeable/experienced enough that I too can help others. [Maybe I will champion answering those long posts you warn against ... ;-) ]
Whenever you have issues with code, please provide the minimum, runnable code to reproduce the problem as well as the input you're giving to reproduce the problem. For example, here's the code I'm using that appears to work
VC1 = {1, 2, 3, 4, 5}

def test():
   D9 = int(input())
   while D9 not in VC1:
       print ("That is not a correct value. Try again.")
       D9 = int(input())
   else:
       print ("You selected", D9)
Interactive session showing use:
Output:
>>> test() 10 That is not a correct value. Try again. -1 That is not a correct value. Try again. 0 That is not a correct value. Try again. 7 That is not a correct value. Try again. 6 That is not a correct value. Try again. 1 You selected 1 >>> test() 2 You selected 2 >>> test() 3 You selected 3 >>> test() 4 You selected 4 >>> test() 5 You selected 5
(Jun-07-2017, 07:32 PM)liamwemyss Wrote: [ -> ]Is there some method that people use to plan out a program prior to just sitting down and writing it. Like Boolean flowcharts or something. Is there a resource I can access to teach how to plan a program. I am EXTREMELY grateful there are those here willing to help with syntax, and hopefully one day I will get knowledgeable/experienced enough that I too can help others. [Maybe I will champion answering those long posts you warn against ... ;-) ]
People do talk about sitting down with pencil and paper before coding, but in practice it's rarely done. I haven't seen tooling for pre-coding prep in spite of it being recommended. Any generic, non-programming flowchart stuff should help though. (I sometimes use a whiteboard, but nothing that sticks around longer.) I think a lot of it is "try and screw up, you'll learn from that" as well as being able to learn from the forum; answering questions that you think you understand helps a lot as well, and people will correct it if it's wrong.
My issue was D9 was defined in the wrong location. This code helped clear it up for me. Thank you!

I was unaware of Cunningham's Law. I am fully familiar with Murphy's Law, but I will have to remember this new one.

Respectfully,

Liamwemyss