在 Python 3.9 中,我正在插入 unicode啤酒杯我得到的灰色菱形数量与正确图片数量相同?

问题描述

我正在编写代码用户输入啤酒瓶的数量输出应该是歌曲的诗句减少到 1。我正在尝试获取正确数量的 unicode 啤酒杯进行打印在诗句之前(5 杯,然后唱 5 瓶啤酒,4 杯,然后是 4 瓶啤酒,等等)。我有 3 个问题:

  1. 我得到相同数量的带问号的灰色菱形
  2. 如果我再次唱这首歌,啤酒杯的最高数量不会打印,我只会得到正确数量的灰色菱形和
  3. (但我不能总是复制这个),如果你输入的年龄在 21 岁以下,并且它会用吸管打印杯子(用于根汁啤酒),我会让这首歌唱得不同。

如果您切换年龄组,它有时会混合打印啤酒杯和吸管杯。它没有明显的模式。

代码如下:

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):   
            
            for count in range(0,i):
                print('?',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!")

解决方法

灰色问号很可能是您的终端没有适当支持 unicode / emojis。 Windows 因这一点而臭名昭著。

我们需要知道 valid_num() 做了什么才能正确评估这一点。

目前还不清楚 age 来自哪里。如果它是 ageCheck() 的参数,则应将其视为一个参数,但如果它是全局变量,PEP8 建议它应全部大写,如 AGE,以提高可读性。

在调试您的代码时,我建议您使用 X 和 O 等非表情符号字符代替啤酒杯和带吸管的杯子,这样您就可以排除 Unicode 奇怪的潜在问题。

另外,我建议交换你的块

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

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

他们完成同样的事情,只是更干净,更少的 I/O 调用