首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >十个绿瓶子程序python

十个绿瓶子程序python
EN

Stack Overflow用户
提问于 2020-10-23 17:19:29
回答 4查看 344关注 0票数 0

我正在用Python编写一个程序,该程序根据用户输入的瓶子数量打印十个绿色瓶子程序。这就是我到目前为止所拥有的。

代码语言:javascript
复制
x=input("How many bottles to start with?\n")                                                    
while x > 0:                                                    
  if(x==1):                                                 
    print (str(x) + " "+"green bottle, hanging on the wall")                                                    
    print (str(x) + " "+ "green bottle hanging on the wall")                                                    
    print ("and if one green bottle, should accidentally falls")                                                    
    x = x - 1                                                   
    print("there'd be no green bottle hanging on the wall")                                                 
    print("\n")                                                 
  elif(x==2):                                                   
    print (str(x) + " "+"green bottles, hanging on the wall")                                                   
    print (str(x) + " "+ "green bottles hanging on the wall")                                                   
    print ("and if one green bottle should accidentally fall")                                                  
    x = x - 1                                                   
    print ("there'd be "  + str (x)  + " green bottle, hanging on the wall")                                                    
    print("\n")                                                 
  else:                                                 
    print (str(x) + " "+"green bottles, hanging on the wall")                                                   
    print (str(x) + " "+ "green bottles hanging on the wall")                                                   
    print ("and if one green bottle should accidentally falls")                                                 
    x = x - 1                                                   
    print ("there'd be "  + str (x)  + " green bottles, hanging on the wall")                                                   
    print("\n")                                                 

问题1:我还需要将“1”改为“1”。我正在考虑写一个函数,因为我只需要把1改成10。请告诉我怎么做。

EN

回答 4

Stack Overflow用户

发布于 2020-10-23 18:15:10

问题1这可以通过一条if语句简单地完成。由于您提到您只需要1到10,因此语句应如下所示

代码语言:javascript
复制
x = int(input("How many bottles to start with? "))
if (x > 10 or x < 1):
    print(f"Can't start with {x} bottles. Pick a number between 1 and 10.")
    exit()

问题2可以用字典来完成的

代码语言:javascript
复制
map_to_str = {
    0: "NO",
    1: "ONE",
    2: "TWO",
    3: "THREE",
    4: "FOUR",
    5: "FIVE",
    6: "SIX",
    7: "SEVEN",
    8: "EIGHT",
    9: "NINE",
    10: "TEN"
}

代码的另一个问题是大量代码不必要地重复。首先,我将使用for循环。我还会写一个函数,以字符串的形式返回瓶子的数量。

代码语言:javascript
复制
def bottles_to_str(num_of_bottles):
    return f"{map_to_str[num_of_bottles]} green bottle" + "s"*(num_of_bottles!=1)

此函数将number作为参数,并使用字典将其转换为字符串。它还检查瓶子的数量是否不等于1 (False也可以解释为0,True也可以解释为1)。如果瓶子数不是1,它的计算结果是"s"*1,它是's',否则它的计算结果是"s"*0,它是空字符串''

主循环将如下所示

代码语言:javascript
复制
for i in range(x, 0, -1):
    print(f"{bottles_to_str(i)} hanging on the wall,")
    print(f"{bottles_to_str(i)} hanging on the wall,")
    print(f"And if one green bottle should accidentally fall,")
    print(f"There'll be {bottles_to_str(i-1)} hanging on the wall\n")
票数 1
EN

Stack Overflow用户

发布于 2020-10-23 17:31:19

要将数字转换为相应的英语单词,建议使用字典:

代码语言:javascript
复制
x = int(input("How many bottles to start with?\n")) # Convert the input to an int
int_to_str = {
1: "one",
2: "two",
...
10: "ten"
}
print ("there'd be "  + int_to_str[x] + " green bottles, hanging on the wall")
票数 0
EN

Stack Overflow用户

发布于 2020-10-23 17:47:10

代码语言:javascript
复制
toAlpha = {0 : "no",
           1 : "one",
           2 : "two",
           3 : "three",
           4 : "four",
           5 : "five",}  # And so on..

def bottleORbottles(x):
    if x > 1:
        return "bottles"
    return "bottle"

x=input("How many bottles to start with?\n")
if x == 0 :
    print("WE DONT HAVE ANY BOTTLES..")
    
while x > 0:
    print((toAlpha[x] + " green " + bottleORbottles(x) + " , hanging on the wall \n") *2)
    print ("and if one green bottle, should accidentally fall")
    x = x - 1
    print("there'd be " + toAlpha[x] + " green  " + bottleORbottles(x) + " hanging on the wall \n")

看看如何通过使用小函数来消除冗余。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64497311

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档