Web Analytics Made Easy -
StatCounter Student needs help - CodingForum


No announcement yet.

Student needs help

  • Filter
  • Time
  • Show
Clear All
new posts

  • Student needs help

    I'm new to this site. I'm student at UCSD taking a beginning Python class. I have to write a code that reads repeatedly numbers input by the user and stops when user types , Stop, Done or Quit. Professor does not want student to use While True function in the loop. I can't get the loop to work. I'm using Python 3.5. I need help with this and any recommendation of a good interactive web site where I can practice, practice and practice !!!!

    Thank you and please hlep

    email: [email protected]

  • #2
    What code have you got so far?

    I can't get the loop to work.
    any recommendation of a good interactive web site where I can practice
    What have you been using to find out it doesn't work?

    And what's wrong with using that?

    And it is just while that is dis-allowed?


    • #3
      If he literally means no "while true" I would do while "condition" where the condition is your exit. This is a case where Pythons lack of a proper do/while is utterly banjaxed and results in more complicated (and slower) code than need be. But to be fair, I don't get why anyone would use Python by choice, it teaches and invites bad practices, and certainly isn't an easy language to use despite the wild claims.

      Something like:
      while (
          userInput != 'quit' and
          userInput != 'done' and
          userInput != 'stop'

      You would initialize the variable to something other than those three values before the while, and assign userInput inside the loop to, well, your user input. (which I'd suggest turning into lower case so the comparisons are case insensitive).

      Note, I may have goofed on the indentation, the use of whitespace as block delimiting is one of the things in python that pisses me off.
      Walk the dark path, sleep with angels, call the past for help.


      • #4
        You can exit your program by entering Stop, Done or Quit input by importing the sys object and calling the exit() method. It is the most reliable, cross-platform way of stopping code execution.

        Following code is a simple solution for your problem :

        import sys
        number=input('Enter the number : ')
        if number == 'Stop' or number=='Done' or number=='Quit':
        print("You are "+number)
        print("Reading Numbers")
        ASP.Net Software Development