简体   繁体   中英

Save/Store a randomly generated value

I have a very simple code to randomly choose a musical key from a list.

How do I store that value so from that point on the program knows the selected key?

import random
majorkeys = ['A major', 'Bb major', 'B major', 'C major', 'Db major', 'D major', 'Eb major', 'E major', 'F major',
             'Gb major', 'G major', 'Ab major']
minorkeys = ['A minor', 'Bb minor', 'B minor', 'C minor', 'Db minor', 'D minor', 'Eb minor', 'E minor', 'F minor',
             'Gb minor', 'G minor', 'Ab minor']
allkeys = majorkeys + minorkeys

#Lydia - in reference to Lydian Mode.
print("""
Hey there, my name is Lydia,
I'm here to help you start a song!
    """)

knownkey = (input("Do you know what key you would like to start with? "))

if knownkey == "no":
    print("\nLet me help you get this song off the ground! Try start with:")
    print(random.choice(allkeys))
else:
    print("That's a great starting point! Nice Work!")

confirmkey = (input("Are you happy to continue with this key? "))
while confirmkey == 'no':
    print("No worries! Lets do:")
    print(random.choice(allkeys)+" instead!")
    confirmkey = (input("Are you happy to continue with this key? "))

if confirmkey == "yes":
    print("Great!")

You have to save it to a new variable like this:

chosenKey = random.choice(allkeys)

Then when you need to print it use:

print(chosenKey)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM