我正在尝试学习“while”循环和计数器。我知道如何在基本级别上使用它们,但我觉得在这种情况下我已经不再使用它们了,而且可能会有一个更好的初学者答案,它仍然使用while循环和if/elif/else语句。
基本上,程序应该根据从0开始的计数器来打印每一句话,并打印sentence1,然后在第四句之后打印合唱...然后,它继续打印接下来的4个句子,然后在最后打印两次合唱。
这就是我现在所处的位置,但就像我提到的,我觉得我已经不再使用while循环了,这让它变得更简单,感觉有点像作弊。
ver1 = ['sentence1', 'sentence2', 'sentence3', 'sentence4']
ver2 = ['sentence5', 'sentence6', 'sentence7', 'sentence8']
chor1= ['chorus1', 'chorus2']
counter = 0
while counter == 0:
print(ver1[0])
counter += 1
while counter == 1:
print(ver1[1])
counter += 1
while counter == 2:
print(ver1[2])
counter += 1
while counter == 3:
print(ver1[3])
counter += 1
#this if statement was my decision just to see if I could use it properly, but I'd like to do the entire thing with while loops if possible....but using if/elif/else statements isn't forbidden.
if counter >= 5:
print(ver1[3])
else:
print(chor1[0])
我用if语句创建了它,但老师问我是否可以尝试用while循环作为家庭作业……下面是我编写的if/elif/else原始语句。
verse1 = "I came home and my dog was gone"
verse2 = "She took my dog and my truck"
verse3 = "oh no she didn't"
chorus = "ohh how times have changed"
truck = 'gone'
dog = 'gone'
if dog == "gone":
print (verse1)
print (chorus)
else:
print(verse3)
if truck == 'gone':
print (verse2)
print (chorus)
else:
print (verse3)
这只是程序的第一部分,因为我不想继续它,浪费我的时间,如果有一个更好的答案,而不是只复制/粘贴while循环,通过几个小编辑来确定打印什么。
https://stackoverflow.com/questions/51314944
复制相似问题