-1

I am writing code to have the user enter the number of bottles of beer on the while and the output should be the verses of the song down to 1. I am trying to get the proper number of unicode mugs of beer to print before the verse (5 mugs, then sing 5 bottles of beer, 4 mugs, then 4 bottles of beer, etc). I have 3 issues:

  1. am getting an equal number of grey diamonds with question marks
  2. if I sing the song again, the top number of beer mugs doesn't print, I just get the correct number of grey diamonds and
  3. (but I can't always reproduce this), I have the song sing differently if you enter an age under 21 and it prints cups with straws (for root beer).

If you switch age groups, it sometimes prints a mix of beer mugs and cups of straws. It does it with no discernible pattern.

Code is as follows:

def sing(num):
    print( "\n" +str(num) +" Bottles of beer on the wall. \n"+ str(num) +"                 Bottles of beer. \nTake one down, pass it around. \n" +str(num-1) +" Bottles of Beer on the wall. \n \n")
   # print("\N{beer mug}")

def lastbottle():
    print("1 Bottle of beer on the wall.\n1 Bottle of beer.\nTake one down, pass it around. \nNo more bottles of beer on the wall! \nGo to the store and buy some more! 99 bottles of beer on the wall!\n")

def singUnderAge(num):
    print( "\n" + str(num) +" Bottles of root beer on the wall. \n"+ str(num) +" Bottles of root beer. \nTake one down, pass it around. \n" +str(num-1) +" Bottles of root beer on the wall. \n \n")
    #print("\N{cup with straw}")

def lastbottleUnderAge():
    print("1 Bottle of root beer on the wall.\n1 Bottle of root beer.\nTake one down, pass it around. \nNo more bottles of root beer on the wall! \n\nYou've had enough sugar for one day. \nNow brush your teeth and go to bed!\n")
    #print("\N{bed}")

def valid_num():
        isValid = False
        while not isValid:
            try:
                num = int(input("How many bottles should we begin with? (1-99) "))
                if num > 0 and num < 100:
                    isValid = True
            except: 
                ("Please enter a number between 1 and 99: ")
        return(num)

def valid_age():
        #isValid = True
        while True:
            try:
                age = float(input("How old are you? "))
                #if age > 0 and age < 120:
                    #isValid = True
            except ValueError: 
                ("Please enter a number for your age: ")
                continue
            else:
                return(age)
                break

def ageCheck(num):
    if age >= 21:
        
        num = valid_num()

        for i in range(num, 1,-1): 
            
            for count in range(0,i):
                print("", end="", flush=True)
            sing(i)    
        else:
            print("", flush=True)
            lastbottle()
            
    else:
        print("You are not old enough to drink beer. Here is a more appropriate version.\n")
                
        num = valid_num()
        
        for i in range(num, 1,-1):   
            
            for count in range(0,i):
                print('', end="", flush=True)
            singUnderAge(i)
        else:
            print("\N{cup with straw}", flush=True)
            lastbottleUnderAge()
            


singSong = True
while singSong:
    age = valid_age()
    ageCheck(age)


    singSong = input("Would you like to perform another song? ")
    no_list = ["No","N","n","Nyet","Nein","Non","no","NO"]
    if (singSong in no_list):
        singSong = False
print("Thank you. Have a nice day!")
Heather M
  • 1
  • 1
  • 3
    [**We don't allow images of text (code/input/output/errors, or otherwise) on Stack Overflow**](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question). Please [edit] your question, pasting all text into the question with [correct formatting](https://stackoverflow.com/editing-help). Questions with images of text/code/errors are routinely closed. Please also check out the [tour] and the [question guide](https://stackoverflow.com/help/how-to-ask) to make sure this & your future questions are suitable for this Q&A. – costaparas Feb 13 '21 at 00:29

1 Answers1

1

The gray question marks thing is very likely your terminal not having proper support for unicode / emojis. Windows is notorious for this.

We'd need to know what valid_num() does to assess this properly.

It's unclear where age is coming from. If it's meant to be an argument to ageCheck(), it should be taken as one, but if it's a global variable, PEP8 suggests that it should be all-caps, like AGE, to help readability.

While debugging your code, I would suggest using non-emoji characters like X and O in place of the beer mug and cup with straw, so you can rule out Unicode-weirdness as a potential issue.

Also, I would suggest swapping your blocks of

for count in range(0,i):
    print("", end="", flush=True)

with

print(""*i, end="", flush=True)

They accomplish the same thing, just cleaner and with fewer I/O calls

Chance
  • 440
  • 2
  • 6
  • Age is a user input, age_check just asks if it is above or below 21 (US drinking age). Valid num just checks if the number of bottles you want to start with is an integer between 1 & 99 inclusive. The lesson is on loops so the For loop gets to stay for now (but I like the *i better for next time.) And it does appear to be Windows Unicode weirdness, the 'X' s worked just fine. So is there any fixing Unicode weirdness? It's an "above & beyond" the requirements for this assignment, so if there isn't, I can live with that. – Heather M Feb 15 '21 at 02:02
  • Unfortunately, most terminals (especially on Windows) handle fancy Unicode characters (like emojis) *very* badly. There's no amount of fancy code tricks that can get that to play nice. The only terminal I've ever seen reliably print Unicode nicely is the default Terminal on Ubuntu (and, I'd assume, most other Linux distros). In some cases, there are "code page" games you can play to trick CMD into showing you what you're after, but it's very unreliable and can often make your regular text not show properly. – Chance Feb 15 '21 at 15:56