samedi 6 mai 2017

While loop and randomized responses in python [on hold]

I'm not understanding while loops/for loops at all. I don't understand what I need to change to get this program to give out a randomly picked cave when giving input, or how to get the program to loop back correctly.

import time
import random

def main():
    displayIntro()
    chooseCave=0
    checkCave=0

def displayIntro():
    print('Bears & Luck')
    print('------------')

    time.sleep(2)
    print('You enter the valley of myth. The supposed land of bears, sentient bears.''\n'
          'It sounds incredibly laughable, but you needed the money.''\n'
          'You reach the end of the pathway and find yourself at the entrance of two caves.''\n'
          'Horrible weather is fastly approaching''\n')


def checkCave():
    cave1=('You meet a friendly bear drinking tea.''\n'
            'The bear smiles warmly and offers you a cup and a biscuit.''\n'
            'You approach the table and sit down wearily.''\n'
            'In a flash the bear throws up its arms.''\n'
            'The bear points to a chest in the corner and says you can take what you want.')

    cave2=('You meet a friendly bear drinking tea.''\n'
            'The bear smiles warmly and offers you a cup and a biscuit.''\n'
            'You approach the table and sit down wearily.''\n'
            'In a flash the bear throws up its arms.''\n'
            'The Bear strikes you down in a furious blow. You dont walk out alive')

    checkCave=random.randint(1,2)
    if checkCave==1:
        print(cave1)
    else:
        print(cave2)


def chooseCave():
    again='y'
    chooseCave=input('Which cave will you enter?''\n'
                      'Enter 1, 2, or q to quit: ')
    while again=='y':
        while chooseCave!='quit':
            if chooseCave=='1':
                print(checkCave)
            elif chooseCave=='2':
                print(checkCave)
            else:
                print('That is not a valid input')
        else:
                print('Goodbye!')
                quit
    else:
        again=input('Do you wish to try again?: ')

chooseCave()

main()




Aucun commentaire:

Enregistrer un commentaire