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).