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
-
first day of tutorials and i'm attempting what should be an easy one.
name = input("What is your name? ")
print("Hello " + name + "!")on inputting an answer (Harry) i receive NameError: name 'Harry' is not defined
from a few different tutorials of python this same example seems to work, but on Pythonista it seems I need to define the variable in advance. Is that right? or am i missing something?
-
Use
raw_input
instead ofinput
– the tutorials are probably using Python 3, which is not completely backwards-compatible with Python 2.7 (which Pythonista is based on). -
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. -
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 -
Thank you
-
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 listelse: #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.
-
Could you post your code in a code block? You can do that with the rightmost button on the toolbar.
-
#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 listelse: #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() -
#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()
-
@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.
-
@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()