-1

Thanks for helping. I've included my questions as comments below.

This is a test/fun project I’m doing to do a coded type-along program that accompanies the song “Friday I’m in love” by The Cure. Basically you can type along to the song as it plays. Basically, you type: “Monday” The programme responds with “Blue”. At the guitar parts, I want the user to hit the enter button (ValueError created) and musical notes will appear on the page using the random script.

import random

print('OOOOOH! Welcome to the Day-bot generator')
print('Choose your day of the week.')
days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']

while True:
        for stanza1 in range(0,5):
            fiil = ['Blue', 'Gray', 'Too', 'I Dont Care About You', 'IM IN LOVE']
            x = input()
            print(fiil[(days.index(x))])

        for stanza1 in range(0,7):

            fiil = ['Fall Apart', 'Break my Heart', 'Break my Heart', 'Doesnt Even Start', 'IM IN LOVE', 'Wait', 'Always Comes too late']
            x = input()     
            print(fiil[(days.index(x))])

if ValueError: # NEED HELP with the CODE here.
            b=['~~~~', '~~♫~~~~~♫','~~~~♩~~♫~', '~~
~~~~♫♫~~', '~~~♫,~~♩~~','~~~~♫', '♬~~~~~', '~~~♬♩~~~~']
            z= random.randint(0,7)
            print(b[z])

Whenever I type a false value. nice squiggly music come out. But...it jumps back to the start of the code everytime I need it to jump back to where the user left off. Thank you for helping. Its my third day writing any kind of code so pls forgive me for any bad questions.

Samuel He
  • 13
  • 2
  • 1
    https://docs.python.org/3/tutorial/errors.html shows how to actually handle errors. `if` isn't right, and is also *outside* your loop. – jonrsharpe Dec 07 '19 at 08:32
  • What you are after is called _exception handling_.after jonsharpe`s link, read (more technical) here: https://docs.python.org/3/c-api/exceptions.html – Patrick Artner Dec 07 '19 at 08:32
  • Ifyou get multiple ones, this might help: [Catch multiple exceptions in one line (except block)](https://stackoverflow.com/questions/6470428/catch-multiple-exceptions-in-one-line-except-block) – Patrick Artner Dec 07 '19 at 08:35

1 Answers1

2

Welcome to SO and congratulations on getting started with your first project. This might sound pedantic but it was helpful that you posted reproducible code. Next time you should make sure your title is an actual question: even something like "Why does my program jumps back to the start of the code?" would be more helpful than what you currently have.

In general, try not to rely on exceptions to guide your control flow. Since you have a whitelist of what you want to accept (days), you can just use a plain if statement.

if x in days: 
    # Do whatever`

Note that you probably want to allow users to type either "Monday" or "monday" so you want to convert the input to lower case.

 if x.lower() in days: 
    # Do whatever`

Since you'll be calling your random notes multiple times you should wrap it in a function.

def printRandomNotes():
    b=['~~~~', '~~♫~~~~~♫','~~~~♩~~♫~', '~~
~~~~♫♫~~', '~~~♫,~~♩~~','~~~~♫', '♬~~~~~', '~~~♬♩~~~~']
    z= random.randint(0,7)
    print(b[z])

So now let us combine the entire program.

import random

print('OOOOOH! Welcome to the Day-bot generator')
print('Choose your day of the week.')
days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']

def printRandomNotes():
    b=['~~~~', '~~♫~~~~~♫','~~~~♩~~♫~', '~~~~~~♫♫~~', '~~~♫,~~♩~~','~~~~♫', '♬~~~~~', '~~~♬♩~~~~']
    z= random.randint(0,7)
    print(b[z])

while True:
    for _ in range(0,5):
        fiil = ['Blue', 'Gray', 'Too', 'I Dont Care About You', 'IM IN LOVE']
        x = input()
        if x.lower() in days:
            print(fiil[(days.index(x.lower()))])
        else:
            printRandomNotes()

    for _ in range(0,7):
        fiil = ['Fall Apart', 'Break my Heart', 'Break my Heart', 'Doesnt Even Start', 'IM IN LOVE', 'Wait', 'Always Comes too late']
        x = input()
        if x.lower() in days:
            print(fiil[(days.index(x.lower()))])
        else:
            printRandomNotes()

This code still isn't DRY but I wouldn't worry about style too much for now. Have fun coding.

Edit (as a response comments):

If you modify the loop variable i in for i in range(...), you'll just have it overwritten with the next yielded value from range. You'll need to control the increment/decrement of the loop variable yourself. In general the pattern looks like the following.

while i < n:
    if condition:
       i += 1
    else:
       # Do not increment i

Since you'll now have a fairly large repeating block of code, it is probably time to wrap it in a function as follows:

# Returns a bool signifying whether the user hit a day of the week
def reply(fill):
    x = input()
    if x.lower() in days:
        print(fiil[(days.index(x.lower()))])
        return True
    else:
        printRandomNotes()
        return False

Putting it all together, it looks like the following:

import random

print('OOOOOH! Welcome to the Day-bot generator')
print('Choose your day of the week.')
days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']

def printRandomNotes():
    b=['~~~~', '~~♫~~~~~♫','~~~~♩~~♫~', '~~~~~~♫♫~~', '~~~♫,~~♩~~','~~~~♫', '♬~~~~~', '~~~♬♩~~~~']
    z= random.randint(0,7)
    print(b[z])

def reply(fill):
    x = input()
    if x.lower() in days:
        print(fiil[(days.index(x.lower()))])
        return True
    else:
        printRandomNotes()
        return False

while True:
    i = 0
    while i < 5:
        fiil = ['Blue', 'Gray', 'Too', 'I Dont Care About You', 'IM IN LOVE']
        success = reply(fiil)
        if success:
            i += 1

    i = 0
    while i < 7:
        fiil = ['Fall Apart', 'Break my Heart', 'Break my Heart', 'Doesnt Even Start', 'IM IN LOVE', 'Wait', 'Always Comes too late']
        success = reply(fiil)
        if success:
            i += 1

Note that this program doesn't enforce the ordering (i.e. it allows the user to type Tuesday before Monday).

Kent Shikama
  • 3,910
  • 3
  • 22
  • 55
  • 1
    Thanks Kent! This helps so much! I'll use your method to edit my full code! Looks like it should work! Also. Thanks for teaching the extra stuff like lower case. I'll go study that! – Samuel He Dec 07 '19 at 10:15
  • Hey Kent. Thanks. the code works better now. I.e. it doesn't jump to the start of the code. But a printRandomnotes() entry still counts as an entry. I'd like it not to count as an entry at all. I.e. I'd want all the "else" entries(k) below to not count at all. Below would be an ideal outcome for me. user: monday Answer: Blue user: k Answer: ~~♫~~~~~♫ user: k Answer: ~~~~♩~~♫~ user: k Answer: ~~~~♩~~♫~ user: k Answer: ~~~~ user: Tuesday Answer: Gray – Samuel He Dec 09 '19 at 12:20
  • Trying something like this: Doesn't seem to work. ` else: printRandomNotes() _ = _ - 1 ` – Samuel He Dec 09 '19 at 15:54