omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    newbie problem

    Pythonista
    print input newbie variables
    6
    11
    9952
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • omz
      omz last edited by

      Use raw_input instead of input – the tutorials are probably using Python 3, which is not completely backwards-compatible with Python 2.7 (which Pythonista is based on).

      1 Reply Last reply Reply Quote 0
      • PythonKing
        PythonKing last edited by

        I have another newbie problem
        if age < 13:
        print ("You're rather young. Is'nt it your bedtime.")

        elif age < 20:
        print ("You are a teenager.")

        elif age < 25:
        print ("Are you at university? I studied how to be a computer.")

        elif age < 200:
        print ("You are so old. When do you retire?")

        else:
        print ("I'm so sorry. I only understand numbers.")
        This code always runs else. Could you please explain why?
        Thank you.

        1 Reply Last reply Reply Quote 0
        • blmacbeth
          blmacbeth last edited by

          raw_input returns a string value. You're if-elif-else statement is expecting an integer value for age. There is a simple fix:

          age = int(raw_input('How old are you?'))
          if age < 13:
          	print ("You're rather young. Is'nt it your bedtime.")
          elif age < 20:
          	print ("You are a teenager.")
          elif age < 25:
          	print ("Are you at university? I studied how to be a computer.")
          elif age < 200:
          	print ("You are so old. When do you retire?")
          else:
          	print ("I'm so sorry. I only understand numbers.")
          

          Notice, you convert the input to an integer using Python's built in int() function. However, this can fail if you input something other than a number, so maybe a try-catch would be better, but that should concern you later in your Python studies.

          Hope this helps,
          B

          1 Reply Last reply Reply Quote 0
          • PythonKing
            PythonKing last edited by

            Thank you

            1 Reply Last reply Reply Quote 0
            • PythonKing
              PythonKing last edited by

              Sorry, me again. I'm working my way through some projects my teacher has set and even though I'm copying what they have written exactly, it still goes wrong. In the following code, in the the first function (make report) when I try to run it, an error message comes up on if telling me i is not defined. How do I correct this. Thank you.

              pupil_details = [["Tom","b","good"],["Pierre","b","bad"],["Hilary","g","ok"],["Victoria","g","bad",],["Jimmy","b","good"],["James","b","ok"],["Alice","g","good"],["Lilly","g","bad"]]

              pupil_gender = ["He","She"]

              pupil_behaviour_good = [" should be rewarded."," adds a whole new dimension to lessons."," knows more than the teacher."," makes me want to cry."," needs to be admired."]

              pupil_behaviour_ok = [" does a good job."," should be pleased."," has a good understanding of the topics."," shows the poor performers what they could become."," should be given a Freddo."]

              pupil_behaviour_bad = [" should be disciplined."," needs to care about their future."," needs extra tutition."," should retake tests."," needs to aim for higher grades."]

              #------Functions-----#

              def make_report(): #This function makes reports.
              for i in pupil_details: #For each list in the pupil_details...
              print ("This is the report for " +i[0]) #Print the title and the pupil's name.

              if i[2] == "good": #If the pupil was behaving good...
              print(i[0] + choice(pupil_behaviour_good)) #Print pupil's name and a random element from the
              #pupil_behaviour_good list.

              elif i[2] == "ok": #If the pupil was behaving ok...
              print(i[0] + choice(pupil_behaviour_ok)) #Print pupil's name and a random element from the
              #pupil_behaviour_good list

              else: #There is only one other option...
              print(i[0] + choice(pupil_behaviour_bad)) #print pupil's name and a random element from the
              #pupil_behaviour_bad list.

              print("\n") #Create a new line before the next report.

              1 Reply Last reply Reply Quote 0
              • dgelessus
                dgelessus last edited by

                Could you post your code in a code block? You can do that with the rightmost button on the toolbar.

                1 Reply Last reply Reply Quote 0
                • PythonKing
                  PythonKing last edited by

                  #This is my Automatic Report Writer Script
                  #It uses lists, choices and If, Elif and Else statements and my own functions!
                  #coding utf-8

                  #-----Import libaries-----#

                  from random import choice

                  #-----Variables------#

                  pupil_details = [["Tom","b","good"],["Pierre","b","bad"],["Hilary","g","ok"],["Victoria","g","bad",],["Jimmy","b","good"],["James","b","ok"],["Alice","g","good"],["Lilly","g","bad"]]

                  pupil_gender = ["He","She"]

                  pupil_behaviour_good = [" should be rewarded."," adds a whole new dimension to lessons."," knows more than the teacher."," makes me want to cry."," needs to be admired."]

                  pupil_behaviour_ok = [" does a good job."," should be pleased."," has a good understanding of the topics."," shows the poor performers what they could become."," should be given a Freddo."]

                  pupil_behaviour_bad = [" should be disciplined."," needs to care about their future."," needs extra tutition."," should retake tests."," needs to aim for higher grades."]

                  #------Functions-----#

                  def make_report(): #This function makes reports.
                  for i in pupil_details: #For each list in the pupil_details...
                  print ("This is the report for " +i[0]) #Print the title and the pupil's name.

                  if i[2] == "good": #If the pupil was behaving good...
                  print(i[0] + choice(pupil_behaviour_good)) #Print pupil's name and a random element from the
                  #pupil_behaviour_good list.

                  elif i[2] == "ok": #If the pupil was behaving ok...
                  print(i[0] + choice(pupil_behaviour_ok)) #Print pupil's name and a random element from the
                  #pupil_behaviour_good list

                  else: #There is only one other option...
                  print(i[0] + choice(pupil_behaviour_bad)) #print pupil's name and a random element from the
                  #pupil_behaviour_bad list.

                  print("\n") #Create a new line before the next report.

                  #------Initialisation------#

                  #------Main Program Code-----#
                  make_report()

                  1 Reply Last reply Reply Quote 0
                  • PythonKing
                    PythonKing last edited by ccc

                    #This is my Automatic Report Writer Script
                    #It uses lists, choices and If, Elif and Else statements and my own functions!
                    #coding utf-8
                    
                    #-----Import libaries-----#
                    
                    from random import choice
                    
                    #-----Variables------#
                    
                    pupil_details = [["Tom","b","good"],["Pierre","b","bad"],["Hilary","g","ok"],["Victoria","g","bad",],["Jimmy","b","good"],["James","b","ok"],["Alice","g","good"],["Lilly","g","bad"]]
                    
                    pupil_gender = ["He","She"]
                    
                    pupil_behaviour_good = [" should be rewarded."," adds a whole new dimension to lessons."," knows more than the teacher."," makes me want to cry."," needs to be admired."]
                    
                    pupil_behaviour_ok = [" does a good job."," should be pleased."," has a good understanding of the topics."," shows the poor performers what they could become."," should be given a Freddo."]
                    
                    pupil_behaviour_bad = [" should be disciplined."," needs to care about their future."," needs extra tutition."," should retake tests."," needs to aim for higher grades."]
                    
                    
                    #------Functions-----#
                    
                    def make_report():                               #This function makes reports.
                    	for i in pupil_details:                        #For each list in the pupil_details...
                    		print ("This is the report for " +i[0])      #Print the title and the pupil's name.
                    
                    		if i[2] == "good":                           #If the pupil was behaving good...
                    			print(i[0] + choice(pupil_behaviour_good)) #Print pupil's name and a random element from the 
                        	                                           #pupil_behaviour_good list.
                        	
                    		elif i[2] == "ok":                           #If the pupil was behaving ok...
                    			print(i[0] + choice(pupil_behaviour_ok))   #Print pupil's name and a random element from the
                        	                                           #pupil_behaviour_good list
                        	
                    		else:                                        #There is only one other option...
                    			print(i[0] + choice(pupil_behaviour_bad))  #print pupil's name and a random element from the
                        	                                           #pupil_behaviour_bad list.
                        	
                    		print("\n")                                  #Create a new line before the next report.
                    
                    #------Initialisation------#
                    
                    
                    
                    
                    #------Main Program Code-----#
                    make_report()
                    
                    Phuket2 2 Replies Last reply Reply Quote 0
                    • Phuket2
                      Phuket2 @PythonKing last edited by Phuket2

                      @PythonKing, look I didn't look at the code cosely. But I think you just have an indent problem in your code. I copied you code and after fixing the indenting it worked.ball the lines under your def main, are at the same level as the def. you need to tab them in one level, then the lines underneath a if for example need to be tabbed in again.

                      def main():
                          If x == 0:
                              pass
                          elif x == 1:
                              pass
                          else:
                              print 'in else'
                      
                      main()
                      
                      

                      I got
                      This is the report for Tom
                      This is the report for Pierre
                      This is the report for Hilary
                      This is the report for Victoria
                      This is the report for Jimmy
                      This is the report for James
                      This is the report for Alice
                      This is the report for Lilly
                      Lilly needs to care about their future.

                      So, just go through your code and make sure everything is tabbed to the correct level. I am sure that's all it is.

                      1 Reply Last reply Reply Quote 0
                      • Phuket2
                        Phuket2 @PythonKing last edited by

                        @PythonKing, but regardless, please consider to start your Python something like

                        # coding: utf-8
                        
                        def main():
                        	print 'i am in the main function'
                        
                        #look this up on the net to see why you do this way
                        if __name__ == '__main__':
                        	main()
                        
                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post
                        Powered by NodeBB Forums | Contributors