#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0

    Question Just started programming


    Code:
    import random
    import time
    
    
    print ('Land of Trees')
    CharChoice = 'pick'
    while CharChoice != 'gunner' and CharChoice!= 'fighter':
        CharChoice = input(' Would you like to play as a fighter or a gunner?')
        if CharChoice == 'gunner':
            print ("you're a gunner")
    
        if CharChoice == 'fighter':
            print ("you're a fighter")
        
    gold = 0
    
    print('You step into the forest')
    print('You see two ways you can go')
    print('way 1 has a small monster, way 2 has a medium monster')
    WaYpIck = input('Which way will you go, way 1 or 2?')
    WaYpIck = int(WaYpIck)
    
    AttaCk = random.randint(4,17)
    
    if WaYpIck == 1:
        print ('You walk up to the small monster and attack')
        SmallMonster = random.randint(1,5)
        if AttaCk > SmallMonster:
            gold = gold + 3
            gold = str(gold)
            print('Congrats you have defeated the small monster and gained 3 gold, your gold count is now ' + gold + '')
    
        if AttaCk < SmallMonster:
            print ('You have died')
    
    if WaYpIck == 2:
        print ('You walk up to the medium monster and attack')
        MediumMonster = random.randint(3,7)
        if AttaCk > MediumMonster:
            gold = gold + 7
            gold = str(gold)
            print('Congrats you have defeated the medium monster and gained 7 gold, your gold count is now ' + gold + '')
    
        if AttaCk < MediumMonster:
            print('you have died')
    I've only been studying for about 2 days after work and I decided to make a text style game with some of the stuff I've learned, the code works so far and i still have a ton of stuff to add, my question is, even if the code is bad, is this okay for only a little bit of study time? I feel like I'm starting to grasp it, i just don't want to think i am when I'm really not , any help or tips would be greatly appreciated!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    76
    Rep Power
    2
    Hey, that's a nice start atleast
    I've "fixed" it abit, some simple stuff you probarly understand. Your game is now runnable in both python2 and python3.

    If I were you, I would make alot of your code into functions, to make the "main code" alot easier to read, and you would save alot of characters and have easier time when you change your code.

    for instance, why not have a input function with an internal loop, which won't stop looping until you have supplied a correct answer? so you could code things like this:

    Character = MyinputFunction(["gunner", "fighter"])

    Code:
    #MAKES THE PRINT FUNCTION IN PYTHON2 WORK AS THE ONE FROM PYTHON3
    from __future__ import print_function
    
    #IMPORTS NECESSARY MODULES
    import random
    import time
    import sys
    
    #MAKES THE INPUT FUNCTION IN PYTHON2 WORK AS THE ONE FROM PYTHON3
    if sys.version_info[0] == 2:
        input = raw_input
    
    #DECLARES SOME VARIABLES
    gold = 0
    
    print ('Land of Trees')
    
    CharChoice = 'pick'
    while CharChoice != 'gunner' and CharChoice!= 'fighter':
        CharChoice = raw_input(' Would you like to play as a fighter or a gunner?')
        if CharChoice == 'gunner':
            print ("you're a gunner")
    
        if CharChoice == 'fighter':
            print ("you're a fighter")
    
    print('You step into the forest')
    print('You see two ways you can go')
    print('way 1 has a small monster, way 2 has a medium monster')
    WayPick = input('Which way will you go, way 1 or 2?')
    WayPick = int(WayPick)
    
    Attack = random.randint(4,17)
    
    if WayPick == 1:
        print ('You walk up to the small monster and Attack')
        SmallMonster = random.randint(1,5)
        if Attack > SmallMonster:
            gold = gold + 3
            gold = str(gold)
            print('Congrats you have defeated the small monster and gained 3 gold, your gold count is now ' + gold + '')
    
        if Attack < SmallMonster:
            print ('You have died')
    
    if WayPick == 2:
        print ('You walk up to the medium monster and Attack')
        MediumMonster = random.randint(3,7)
        if Attack > MediumMonster:
            gold = gold + 7
            gold = str(gold)
            print('Congrats you have defeated the medium monster and gained 7 gold, your gold count is now ' + gold + '')
    
        if Attack < MediumMonster:
            print('you have died')
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by leonnaley2
    Hey, that's a nice start atleast
    I've "fixed" it abit, some simple stuff you probarly understand. Your game is now runnable in both python2 and python3.

    If I were you, I would make alot of your code into functions, to make the "main code" alot easier to read, and you would save alot of characters and have easier time when you change your code.

    for instance, why not have a input function with an internal loop, which won't stop looping until you have supplied a correct answer? so you could code things like this:

    Character = MyinputFunction(["gunner", "fighter"])
    Thanks for the info! I'm just now learning how to create my own functions, it's still a bit confusing. (Invent with python) is the tutorial I've been using.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    451
    Rep Power
    32
    The while loop can be simplified with
    Code:
    while CharChoice != 'gunner' and CharChoice!= 'fighter':
        CharChoice = input(' Would you like to play as a fighter or a gunner?')
    print ("you're a " + CharChoice)
    and you should test for an integer before converting. Also, what happens if Attack ==SmallMonster? You want an if / else there. So greater than, and the else covers < or ==.
    Code:
    WayPick = input('Which way will you go, way 1 or 2?')
    #
    # becomes
    WayPick = 0
    while WayPick not in ["1", "2"]:
        WayPick = input('Which way will you go, way 1 or 2?')
    WayPick = int(WayPick)
    #
    # or
    if WayPick.isdigit():  ## but will also allow 3, 4, 5, etc.
        WayPick = int(WayPick)
    Last edited by dwblas; July 12th, 2013 at 11:15 AM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by dwblas
    The while loop can be simplified with
    Code:
    while CharChoice != 'gunner' and CharChoice!= 'fighter':
        CharChoice = input(' Would you like to play as a fighter or a gunner?')
    print ("you're a " + CharChoice)
    and you should test for an integer before converting. Also, what happens if Attack ==SmallMonster? You want an if / else there. So greater than, and the else covers < or ==.
    Code:
    WayPick = input('Which way will you go, way 1 or 2?')
    #
    # becomes
    WayPick = 0
    while WayPick not in ["1", "2"]:
        WayPick = input('Which way will you go, way 1 or 2?')
    WayPick = int(WayPick)
    #
    # or
    if WayPick.isdigit():  ## but will also allow 3, 4, 5, etc.
        WayPick = int(WayPick)
    Thx, so much to learn i feel like when i can start making functions I'll be better off

IMN logo majestic logo threadwatch logo seochat tools logo