While循环在Python中不能正常工作

While循环在Python中不能正常工作,python,while-loop,Python,While Loop,所以我是Python的noob&我正在为我的学校做一个项目,所以我遇到了While循环的问题,语音API(espeak)再次重复第12个选项,它不断重复,直到我被迫退出。这是我添加espeak的部分 import pyttsx3 import speech_recognition as sr engine = pyttsx3.init('espeak') voices = engine.getProperty('voices') engine.setProperty('voice', voic

所以我是Python的noob&我正在为我的学校做一个项目,所以我遇到了While循环的问题,语音API(espeak)再次重复第12个选项,它不断重复,直到我被迫退出。这是我添加espeak的部分

import pyttsx3
import speech_recognition as sr

engine = pyttsx3.init('espeak')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[2].id)

def speak(audio):
    engine.say(audio)
    engine.runAndWait()
&这是while循环部分

while choice != 17:
    if choice == 1:
        print("""
        If you want to make a website, you can see this site
        https://websitesetup.org/
        If you want to make a website you might learn HTML (Hyper Text Markup Language). It is the core of the website. You also need to learn CSS (Cascading Style Sheets) for making website responsive & beautiful & JS (JavaScript) to make website functional properly. CSS & JS are not required but recommended.
        """)
        speak("""
        If you want to make a website, you can see this site
        https://websitesetup.org/
        If you want to make awhile website you might learn HTML (Hyper Text Markup Language). It is the core of the website. You also need to learn CSS (Cascading Style Sheets) for making website responsive & beautiful & JS (JavaScript) to make website functional properly. CSS & JS are not required but recommended.
        """)

    if choice == 2:
        print("""
        If you want to make an application, you need to know that we can make application either for Android or iOS. For Android apps there are many platforms like Plezmo, MIT App Inventor, MIT Scratch etc. They use the block coding... If you want to make text coding then you can use Java. If you want to make iOS apps then you need to have a mac device & you can program in Swift language.
        You can also see: https://www.biznessapps.com/blog/how-to-build-a-mobile-app-in-12-easy-steps/
        """)
        speak("""
        If you want to make an application, you need to know that we can make application either for Android or iOS. For Android apps there are many platforms like Plezmo, MIT App Inventor, MIT Scratch etc. They use the block coding... If you want to make text coding then you can use Java. If you want to make iOS apps then you need to have a mac device & you can program in Swift language.
        You can also see: https://www.biznessapps.com/blog/how-to-build-a-mobile-app-in-12-easy-steps/
        """)
        
    if choice == 3:
        print("""
        If you want to learn Machine Learning, then you can learn Python. There are many concepts of Machine Learning, & the learning curve is very huge so it is not very possible to learn all the concepts of Machine Learning. There are many modules to learn for Machine Learning like Scikit-learn, Theano, Tensorflow etc. They all have different jobs. If you want to have a simple idea of Machine Learning then you can try out Teachable Machine (https://teachablemachine.withgoogle.com/) for having a simple idea of Machine Learning. 
        You can also see: https://www.geeksforgeeks.org/how-to-start-learning-machine-learning/
        """)
        speak("""
        If you want to learn Machine Learning, then you can learn Python. There are many concepts of Machine Learning, & the learning curve is very huge so it is not very possible to learn all the concepts of Machine Learning. There are many modules to learn for Machine Learning like Scikit-learn, Theano, Tensorflow etc. They all have different jobs. If you want to have a simple idea of Machine Learning then you can try out Teachable Machine (https://teachablemachine.withgoogle.com/) for having a simple idea of Machine Learning. 
        You can also see: https://www.geeksforgeeks.org/how-to-start-learning-machine-learning/
        """)
    if choice == 4:
        print("""
        If you want to learn Hacking, you need to learn about Linux (Command Line), Networking, Python, some modules of Python like Scapy, Requests, Cryptography etc. Also you have three types of Hackers available.
        1. Black Hat Hackers, those who do hacking for malicious intenet & have a intent to harm the computer. They do things for illegal things.
        2. White Hat Hackers, also known as Ethical Hackers are those who use the same technology as Black Hat Hackers, but for a good intent, for the security of a Computer, & to get track of Black Hat Hacker
        3. Grey Hat Hackers, are a mix of both, they do things to check the security of the system but without the user awareness, but do not have an intent to harm the computer. 
        """)
        speak("""
        If you want to learn Hacking, you need to learn about Linux (Command Line), Networking, Python, some modules of Python like Scapy, Requests, Cryptography etc. Also you have three types of Hackers available.
        1. Black Hat Hackers, those who do hacking for malicious intenet & have a intent to harm the computer. They do things for illegal things.
        2. White Hat Hackers, also known as Ethical Hackers are those who use the same technology as Black Hat Hackers, but for a good intent, for the security of a Computer, & to get track of Black Hat Hacker
        3. Grey Hat Hackers, are a mix of both, they do things to check the security of the system but without the user awareness, but do not have an intent to harm the computer.
        See this site for more info: https://www.wikihow.com/Start-Learning-to-Hack
        """)
    if choice == 5:
        print("""
        If you want to learn Python, then you can refer to some tutorials which are made for Python. Python is a programming language which has an easy to understand syntax & can be used in many fields like Machine Learning, Game Development, GUI Development, Website Development, etc. It is one of the most wanted languages in demand (According to Stack Overflow). Many companies use this language like Netflix, Google, Instagram etc,
        Refer to this site for learning Python: https://www.w3schools.com/python/
        """)
        speak("""
        If you want to learn Python, then you can refer to some tutorials which are made for Python. Python is a programming language which has an easy to understand syntax & can be used in many fields like Machine Learning, Game Development, GUI Development, Website Development, etc. It is one of the most wanted languages in demand (According to Stack Overflow). Many companies use this language like Netflix, Google, Instagram etc,
        Refer to this site for learning Python: https://www.w3schools.com/python/
        """)
    if choice == 6:
        print("This is the site for ATL Innovation Mission: https://aim.gov.in/")
        speak("This is the site for ATL Innovation Mission: https://aim.gov.in/")
    if choice == 7:
        print("Website is slow due to a technical issue at their site only, so kindly have patience & it will load soon!")
        speak("Website is slow due to a technical issue at their site only, so kindly have patience & it will load soon!")
    if choice == 8:
        print("This is a technical issue from their site itself. Due to a lot of traffic sometimes the site doesn't open. So you can wait for sometimes or try at one of the times like early morning or late night, so there might be less chances for having the site unusable. Sometimes the site is made for some browser online like Google Chrome or Mozilla Firefox, so if it doesn't open try from any other browser.")
        speak("This is a technical issue from their site itself. Due to a lot of traffic sometimes the site doesn't open. So you can wait for sometimes or try at one of the times like early morning or late night, so there might be less chances for having the site unusable. Sometimes the site is made for some browser online like Google Chrome or Mozilla Firefox, so if it doesn't open try from any other browser.")
    if choice == 9:
        print("The website has issues with the login also. It also turns short password into long due to security issues. Login issues are sometimes because of the browser. For example, Brave browser is not compatible & it will show invalid password even though it is correct. 2 browsers working are Google Chrome & Mozilla Firefox")
        speak("The website has issues with the login also. It also turns short password into long due to security issues. Login issues are sometimes because of the browser. For example, Brave browser is not compatible & it will show invalid password even though it is correct. 2 browsers working are Google Chrome & Mozilla Firefox")
    if choice == 10:
        print("To try to repair or improve something without having the proper skill or knowledge")
        speak("To try to repair or improve something without having the proper skill or knowledge")
        if choice == 11:
            print("""
Corruption
Illiteracy
Education System
Basic Sanitation
Healthcare System
Poverty
Pollution
Women’s Safety
Infrastructure
Unemployment
Agricultural Distress
The rise in Global Protectionism
            """)
        speak("""
Corruption
Illiteracy
Education System
Basic Sanitation
Healthcare System
Poverty
Pollution
Women’s Safety
Infrastructure
Unemployment
Agricultural Distress
The rise in Global Protectionism
        """)

    if choice == 12:
        print("""
Step 1: Identify & Meet with Stakeholders. A stakeholder is anyone who is affected by the results of your project plan. ...
Step 2: Set & Prioritize Goals. ...
Step 3: Define Deliverables. ...
Step 4: Create the Project Schedule. ...
Step 5: Identify Issues and Complete a Risk Assessment. ...
Step 6: Present the Project Plan to Stakeholders.
        """)
    speak("""
Step 1: Identify & Meet with Stakeholders. A stakeholder is anyone who is affected by the results of your project plan. ...
Step 2: Set & Prioritize Goals. ...
Step 3: Define Deliverables. ...
Step 4: Create the Project Schedule. ...
Step 5: Identify Issues and Complete a Risk Assessment. ...
Step 6: Present the Project Plan to Stakeholders.
    """)

    if choice == 13:
        print("""
Come up with a simple project.
Get the software you'll need.
Join communities about how to start coding.
Read a few books.
How to start coding with YouTube.
Listen to a podcast.
Run through a tutorial.
Try some games on how to start coding.
Take a Coding Bootcamp
You can also learn coding from these Coding Classes and Programs
Codecademy. ...
Udemy. ...
Skillcrush's FREE Coding Camp. ...
freeCodeCamp. ...
Khan Academy. ...
Web Fundamentals. ...
w3schools. ...
Code.org.etc
        """)
        speak("""
Come up with a simple project.
Get the software you'll need.
Join communities about how to start coding.
Read a few books.
How to start coding with YouTube.
Listen to a podcast.
Run through a tutorial.
Try some games on how to start coding.
Take a Coding Bootcamp
You can also learn coding from these Coding Classes and Programs
Codecademy. ...
Udemy. ...
Skillcrush's FREE Coding Camp. ...
freeCodeCamp. ...
Khan Academy. ...
Web Fundamentals. ...
w3schools. ...
Code.org.etc
        """)

    if choice == 14:
        print("""
    No, coding is not hard to learn. However, like anything new, it's not easy to start, and how difficult a time one has with learning to code will vary across a number of factors. The point is, learning to code isn't impossible; or, it's not as impossible as it might seem when it comes to getting your kids involved.
        """)
        speak("""
    No, coding is not hard to learn. However, like anything new, it's not easy to start, and how difficult a time one has with learning to code will vary across a number of factors. The point is, learning to code isn't impossible; or, it's not as impossible as it might seem when it comes to getting your kids involved.
        """)
    
    if choice == 15:
        print("""
    The job can be stressful at times, but computer programmers are compensated well for any anxiety they might experience. Many jobs in this profession are being outsourced to other countries where pay is lower, saving companies money.
        """)
        speak("""
    The job can be stressful at times, but computer programmers are compensated well for any anxiety they might experience. Many jobs in this profession are being outsourced to other countries where pay is lower, saving companies money.
        """)

    if choice == 16:
        print("""
    Computer programmers get paid well, with an average salary of $63,903 per year in 2020. Beginner programmers earn about $50k and experienced coders earn around $85k.
        """)
        speak("""
    Computer programmers get paid well, with an average salary of $63,903 per year in 2020. Beginner programmers earn about $50k and experienced coders earn around $85k.
        """)

if choice == 17:
    print("Ok Exiting...")
    speak("Ok exiting...")
&这是第12种选择

    if choice == 12:
        print("""
        Step 1: Identify & Meet with Stakeholders. A stakeholder is anyone who is affected by the results of your project plan. ...
        Step 2: Set & Prioritize Goals. ...
        Step 3: Define Deliverables. ...
        Step 4: Create the Project Schedule. ...
        Step 5: Identify Issues and Complete a Risk Assessment. ...
        Step 6: Present the Project Plan to Stakeholders.
        """)
        
    speak("""
        Step 1: Identify & Meet with Stakeholders. A stakeholder is anyone who is affected by the results of your project plan. ...
        Step 2: Set & Prioritize Goals. ...
        Step 3: Define Deliverables. ...
        Step 4: Create the Project Schedule. ...
        Step 5: Identify Issues and Complete a Risk Assessment. ...
        Step 6: Present the Project Plan to Stakeholders.
        """)

您应该在某个地方更新循环中的
choice
。如果
choice
不等于
17
的情况下,while条件将无限重复。

我建议在while循环的开头插入
guess
变量,在任何不是
17
的输入之后,它将循环回while循环的开头,这将给您另一个选择其他选项的机会

while True:
  guess = int(input('input message'))
  if guess == 1:
    # Do something...
  elif guess == 2:
    # Do something else...
  elif guess == 3:
    # Do something else again...
  elif guess == 4:
    # Quit the program for example...

另一种方法是按照Code2D的建议将
从循环中断开,但只有在只选择1个选项后才想结束循环。

正如其他人所说,您需要从循环中更新
选项
变量。一旦循环开始,将只执行其中的内容,在您的情况下,
choice
在循环之前被赋予一个值,因此它保持相同的值(从不为17)并重复

您应该做的是在循环中添加更新选项的方法:

。。。
而选择!=17:
choice=int(输入(“您的选择是什么?”)
...
...

您没有发布整个脚本,因此我假设您在循环之前给了
choice
一个值。将该赋值移到循环的开头;如果出于某种原因,在循环之前需要它,您可以将赋值放在循环的末尾,这样当它第一次运行时,它不会直接要求您做出选择,只有在它完成之后。

缩进关闭。还有什么是
选择
以及如何获得其价值。邮递没有必要显示所有17个几乎相同的选项。当然,所有这些重复的代码都可以简化。你永远不会在循环中更新
choice
,我认为如果在if语句之后添加
break
,那么它可能会工作