【问题标题】:Stop main() function from starting automatically停止 main() 函数自动启动
【发布时间】: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


【解决方案1】:

您可以简单地在主函数中添加另一个提示,即:

from sys import exit

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

        print("Do you want to run the program again?")
        print("Press 0 to exit")
        print("Press 1 to continue")

        choice = int(input("Press number:"))
        if choice == 0:
            print("Exiting")
            exit(0)


main()

【讨论】:

  • 将main() 包装在while 循环中可能比在其中添加另一个范围要好得多!
  • 非常感谢克里斯托弗,它成功了
猜你喜欢
  • 1970-01-01
  • 2020-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-19
  • 2012-08-05
  • 1970-01-01
  • 2020-04-23
相关资源
最近更新 更多