【发布时间】:2021-06-30 09:44:25
【问题描述】:
程序第一次运行后(当我输入选项时)主功能再次自动启动,我想让它不自动启动,而是问你是否想再次运行程序或退出程序,如果我选择运行程序,则 main() 函数再次启动,否则程序结束。
file=open("nomobile.txt","w")
text=" If The worLd was Ending , yOU WIll come over right, u will come over and stay the 9it"
file.write(text)
file=open("nomobile.txt",'r')
characters=file.read()
vowels=0
consonants=0
uppercase=0
lowercase=0
for ch in characters:
if ch.islower():
lowercase+=1
elif ch.isupper():
uppercase+=1
ch=ch.lower()
if ch in "aeiou":
vowels+=1
elif ch in "bcdfghjklmnpqrstvwxyz":
consonants+=1
file.close()
def main():
while True:
print("What do you want to know about?")
print("Press 1 to know about uppercase letters")
print("Press 2 to know about lowercase letters")
print("Press 3 to know about vowels")
print("Press 4 to know about consonants")
print("Press 5 to know about all of them at once")
choice=int(input("Press number:"))
if choice == 1:
print("The number of uppercase letters present are", uppercase)
elif choice == 2:
print("The number of lowercase letters present are", lowercase)
elif choice == 3:
print("The number of vowels present are",vowels)
elif choice == 4:
print("The number of consonants present are",consonants)
elif choice == 5:
print("The number of uppercase letters present are", uppercase)
print("The number of lowercase letters present are", lowercase)
print("The number of vowels present are",vowels)
print("The number of consonants present are",consonants)
else:
break
main()
【问题讨论】:
-
您有问题吗?
-
问题是这样读取一个文本文件并显示文件中元音辅音大写/小写字符的数量。但我需要让它菜单驱动,但是当我第一次运行代码时,main() 函数在给出输出后再次启动
-
如果输入 1 到 5 以外的任何数字,您的程序应该退出。
标签: python function file if-statement while-loop