hello there, well im back;) with a few more questions that i would like clarification with. I have completed up to data structures in python and my next topic is 'object oriented programming':| . Just wanted to clear up some things first:)

guess=int (input ('enter a number:'))

Q1. in this line there is no 'print' function yet 'enter a number' is printed onto the screen and why is there an 'int' preceding that line? is that necessary to specify what type of input is taken from the user?


while run:
	guess=int (input('enter an age:'))
	if guess==age:
		print ('congrats you got it')
	elif guess<age:
		print ('thats too low..')
		print ('thats too high')
print ('while loop is now over..')

Q2. I understand what happens in the above, but..what exactly does 'while run:' tell the computer?, what is 'true' exactly?, in order for that loop to run..? (sorry if im vague i think i confused my self here...).

while True:
s=input ('enter something:')
if s=='quit':
break #breaks program completely, and quits from console
if len(s)<3:
print ('strig length is too small')
print ('the entered input is long enough')

Q3. here, why is continue used?, cant we just say else: print('the entered input is long enough')?. How does a continue statement come in handy?

#"local variables"/functions3
def func(x):
	print ('x is',x)
	print ('changed local x to', x)
print ('x is still', x)

Q4. What is considered local in this case?, Isn't a variable inside a function considered to be local? because if the 'x=2' was not there it will print '50' nonetheless...??

def func():
	global x
	print ('x is:',x)
	print ('changed global x to:',x)
print ('value of x is:',x)

Q5. This seems to be the same as the previous but it calls for x right??, yet in the previous example to print out the value of x 'global' is not needed. How does that work?

-Thank you

Q1 - Whatever you put in the inputs brackets are made into the prompt given to the user when it asks for the input.

Q2 - For a while loop, the loop goes while the value given is true, so run is equal to True so therefore it will run UNTIL the value of run is changed to False

Q3 - Continue, that keyword is used in loops, what is does is skip the rest of the loop and start on the next iteration.

Q4 - x is a global and local variable, it is in the global scope as well as in the function, you supply the value x so in the function is starts as x being 50 but then you change it to x, but that dosent change the value of x outside the function.

Q5 - In the last one it had x in the arguments needed for the function, but then in the next one you dont need to supply x because you make it global with the global x thing, now x in the function is the SAME x as x in the rest of the program.

Hope that helps

re Q1:
In this case you want to make sure that guess will be an integer, even if the user enters a floating point number.

Actually with Python30 the int() becomes very important, since input() now returns a string.

re Q5:
This shows you the danger of using global. When you change x=2 in the function, than the x outside the function will also change.

Just adding on to what paulthom said...

Q1 - There is something a little odd there. The input command will present a prompt to the user and accept their input as an integer. You can compare it to raw_input which does the same thing but treats the users response as a string of characters rather than an integer. When you put int in front of a variable like that, you're telling python to try and convert the answer into an integer...but in this case input will give back an integer anyway. So surrounding it with int() is probably unnecessary.

Q3- I feel like adding more to the discussion on the difference between continue and break. As already mentioned, when python is running in a loop and it encounters the word continue, it will stop running the instructions in the rest of the loop for this pass, and will start with the next pass. So in the following piece of sample code, python will loop through the whole string and check each character. If the character is not an A, it will do nothing and go on to the next character. If the character is an A it will print something and then move on.

listoflettters = 'The quick brown fox jumped over the lazy dog'
for eachletter in listofletters:
    if eachletter != 'a':
    print eachletter

Break, on the other hand, will stop running the instructions in this loop of code, and will move on to the next instruction after the loop. Use break when you dont want to go through the loop anymore.

Q4 - think of the x inside of the function as a completely different x than the one used outside of the function. Inside the function you're saying "here is another variable that I'll call x, and I want the value to be 50." Then you change the value of that x to 2, but that change has no effect on the x that exists outside of your function. It is very important that you know about these scoping issues, but a tip I would give for real programs is that you don't use the same name for variables inside of functions that you've already used outside. You might debug your code for hours until you figure out that you've got a scoping problem.

thanks alot guys. Just one more thing with the

int (input('enter an age:'))

if say the user is asked to input a string with number. Suppose it was "Its finally 2009!'. How would you assign the integer in that string to a variable?
Edit: this is just a general question, but do you find your selves referring back to notes and examples?

just a couple more if you guys dont mind (do let me know if im going over board with the questions.):)

if __name__ == '__main__':
    print('This program is being run by itself')
    print('I am being imported from another module')

Q6. What is the purpose of this example?. How does it work, i dont see a _name_==_main_ anywhere...

myitems =[' phone',' computer',' stuff',' movies',' counter strike']
print('\ni have this many items',len(myitems))

print('\nthese items are',end='')
for item in myitems:
	print(item, end='')
print('\n\nI need a new monitor')

print('\nIf i had a new monitor my new possetions will be.')

for item in myitems:
	print(item, end='')
print ('\nthe Item i use the most is ',myitems[1])

print('\n\nErasing item [0]')

del myitems[0]
print (myitems)

Q7. what does

for item in myitems:
print(item, end='')

do?. Also it starts with 'for item' yet we have not introduced anything called item into our code?.

#'tuples'-data structures

zoo = ('python', 'elephant', 'penguin')
print('Number of animals in the zoo is', len(zoo))

new_zoo = ('monkey', 'camel', zoo)
print('\nNumber of cages in the new zoo is', len(new_zoo))
print('\nAll animals in new zoo are', new_zoo)
print('\nAnimals brought from old zoo are', new_zoo[2])
print('\nLast animal brought from old zoo is', new_zoo[2][2])
print('Number of animals in the new zoo is', len(new_zoo)-1+len(new_zoo[2]))

Q8. I need help understanding the last four lines in this.

name='Ronan dex' #the string object

if name.startswith('Ron'):
	print('yes it starts with "Ron"')

if 'a' in name:
	print('yes there is an "a" in the name')
if name.find('dex') !=-1: 
	print('yes it contains "dex"')
#--------------Second part-----------------

Q9. how do we interpret '!=-1: '? also what is the purpose of 'delimiter'?

-thank you all

Q6 - __name__ is as it says the name of the program, when you run the program that program is given the name '__main__' as it is the main program, but other things that you import are given different names, so the reason people do this is so that if you ever want to import that program into another that the original program dosent run.

Q7 - The for keyword is used to iterate through values, so if myvalues was a list then a loop would begin where the variable item would start off being the first value of myvalues then the loop would run through, then the next value of myvalues would replace the current value of item with its own.
So for example you know a range() makes a range of numbers fror a-b. SO

for num in range(1,11):
    print(num, end = "")
#it would print

So did you see that it ran the loop for every value in the range? cause thats what it does.

Oh delimiter is just a string, so you can have a list lets say like this:

ourList = ["hello", "World", "Its","a","nice","day]
#and then we can go
#that will join every item in the list my adding them all together with nothing in the middle 
#we can use a string variable to
delimiter = '-'
#this will print

!= means does not equal, so if so and so does not equal minue one then the value is true

Also it starts with 'for item' yet we have not introduced anything called item into our code?.

Just to add to the answer given for this question. This is a great example of where local scope comes into play. You're right that the code hadn't introduced a variable called item yet. When you see a piece of code like this:

myword = "secretary"
for letter in myword:
    print letter

Python interprets that as create a variable called letter that will have scope in my for loop. Assign that variable the value in each item of the variable myword.

just to get a clue of things:

string = raw_input("Enter Number: \n")
print type(string)

integer = int(string)
print type(integer)

floating = float(string)
print type(floating)

Thank you everyone.

Thank you everyone.

You are welcome!

Daniweb didn't allowed me to edit my mistake so I will repost the code
just to get a clue of things:

string = raw_input("Enter Number: \n")
print type(string)

integer = int(string)
print type(integer)

floating = float(string)
print type(floating)

hello again.

How do i append the contents of three lists into one list?


print (all)

when i do this i get the

, how do i get rid of the '[]' and just append the numbers them selves into 'all' with just the numbers inside ''?

Also after adding the contents into 'all' how would i completely erase the contents of list1,2,3?.

Very simple ...

list1 = ['1','2','3']
list2 = ['4','5','6']
list3 = ['7','8','9']

all = list1 + list2 + list3

print(all)  # ['1', '2', '3', '4', '5', '6', '7', '8', '9']

del list1
del list2
del list3

Note: You should open a new thread for unrelated questions and give the thread a meaningful title. For instance "Combining Lists"

thanks vegaseat, ah just one more thing


def func():
	print (all)
	del list1
	del list2
	del list3
	print (list1)

how would i do it if it was within a function?
(sorry for that, I didn't think i needed to start a new thread so i just posted here...)

You don't need a new thread, it just makes it easier for someone else to search for this when they have the same problems/questions down the road. To put this in a function you could do something like this

def AddItUp(one, two, three):
    return one + two + three

list1 = ['one','two','three']
list2 = ['four','five','six']
list3 = ['seven','eight','nine']

list4 = AddItUp(list1, list2, list3)

print list4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.