Hi all.

I am a newcomer to python and I am teaching myself from books, videos and on-line tutorials.

I currently have a basic script I am working on (can't post the code right now as I'm away from my laptop), and I cannot figure out how to do what I want to do.

Basically it's a little game where you buy and sell shares. The script displays a message when you begin and asks if you want to continue. If you press y then it goes into a while loop that means that the code will keep running untill you run out of money. When you do run out of money the game ends and as a consequence the script ends.

What I am trying to do is get the script to run from the top again instead of quitting. I'm currently not at the level of classes or functions if that's what it requires, though if it does need them then ill just leave it as is untill I get that far.

Thanks for reading.

4 Years
Discussion Span
Last Post by jtaylor-bye

You just indent code inside another while loop.
However using functions and good descriptive names according to PEP8 guidelines is good to have in your toolbox before tackling big projects. It is good to form correct habits from begining.


Well I have tried using another while loop and i'm probably doing it wrong.

Here is my code for my basic game, and i know it can be improved upon but i'm not at that level yet. Any ideas or help is much appreciated.

Oh and why do then weird symbols keep appearing by my £ signs?

import os
import time
import random

day = 0
money = 500
rolls_royce_share_amount = 500
print ("""You have £500 starting fund.
Make it last as long as you can without losing it all.
You can buy and sell shares for as long as you have money.
Are you ready to continue?""")
start = input().lower()

if start == 'n':
    print ("Bye")

while money >0:

    if start == 'y':
        os.system("cls") # Clears the screen
        print ("You have been playing for : " + str(day) + " days\'s.")
        print ("\nAnd you have, £" + str(money) + " in your account.")
        print ("\nPress :")
        print ("\n1 : To see a list of shares to buy.")
        print ("2 : To see what shares you own.")
        print ("3 : To sell shares you own.")
        print ("4 : To quit the game.")
        selection = input("\nMake your selection : ")

    if selection == '1':
        print ("Here is a list of the companies whose shares you can purchase.")
        print ("\n\n1 : Rolls Royce\n2 : Fairfax Meadow\n3 : Dixons\n4 : Curry\'s\n5 : Aldi\n6 : Sainsbury\'s.")
        company_selection = input("\nPress the number of the company for more details.")

        if company_selection == '1':
            rolls_royce_base = 50
            rollsroyce_modifier = random.randrange(-50, 50)
            rolls_royce_cost = rolls_royce_base + rollsroyce_modifier
            print ("This is the information page for Rollys Royce.")
            print ("\nHere you can see how many shares are available to purchase and at what price.")
            print ("\nThere are, " + str(rolls_royce_share_amount) + " shares to buy.")
            print ("\nEach share costs, £" + str(rolls_royce_cost)+ " to purchase.")
            rolls_buy = input("\nDo you wish to purchase some shares, y/n (n will return to main screen)? : ").lower()

            if rolls_buy == 'y':
                afford_rolls_royce = money // rolls_royce_cost
                print ("You can afford to purchase, " + str(afford_rolls_royce) +" shares.")
                rolls_buy_amount = input("\nEnter an amount of shares to purcahes : ")
                rolls_royce_amount_cost = int(rolls_buy_amount) * rolls_royce_cost
                money = money - rolls_royce_amount_cost
                print ("\nYou have purcahsed, " + str(rolls_buy_amount) + " share/s. At £" + str(rolls_royce_cost) + " each. Costing a total of £" + str(rolls_royce_amount_cost) + ".")
                print ("\nPress any key to continue:")
                day = day + 1
                rolls_royce_share_amount = rolls_royce_share_amount - int(rolls_buy_amount)

    if selection == '4':
        print ("\nGood Bye and thank you for playing!")

Edited by jtaylor-bye


Sorry for the double post but i think i have sorted it out.

while True:

Just by adding that line after the imports.
Is this good or poor coding?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.