Jump to content

Very simple.. but I dont get it!

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
_Metal_

_Metal_

    Newbie

  • Members
  • PipPip
  • 10 posts
Hi!

First of all I just want you to know that I'm from sweden so my english may not be so perfect :c-cool:
And I can not make my python work so I can not check the answer by myself :(

But the question is :

Which of the following piece of code will print all 70 words on the list, "svar"?

n = 0
s = ""
while n < 70:
"indent" s += svar[n]
"indent" n+.....


The problem is solved!:c-grin:

But I have another one..

Which of the following code are loading five words to the list "svar"?

[B]k = 1
while k < 5:
"indent" svar[k] = input(”Ge ord: ”)
"indent" k += 1



k = 1
svar[0] = input(”Ge ord: ”)
while k < 5:
"indent" svar[k] = input(”Ge ord: ”)
"indent" k += 1





k = 0
while k <= 5:
"indent" svar[k] = input(”Ge ord: ”)
"indent" k += 1

k = 0
while k < 5:
"indent" print(”Ge ord: ”)
"indent"svar[k] = input()
"indent" k += 1

k = 0
s1 = ””
while k < 5:
"indent" svar[k] = input(”Ge ord: ”)
"indent" s1 += svar[k]
"indent" k += 1

Edited by _Metal_, 24 December 2010 - 07:57 AM.


#2
_Khalil_

_Khalil_

    Newbie

  • Members
  • Pip
  • 9 posts
I am new so I may just be doing it wrong. But when I run this code it just comes out with a bunch of errors.

#3
whitey6993

whitey6993

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 437 posts
Here's how I see each of these pieces of code:

The first method to print the words will work, as it goes through and adds each word in svar to a string, then outputs the string at the end. The only problem you will encounter is that your words will all run together because there is no space separating them (unless the words in the list actually have spaces in them).

The second method won't print all the words because python lists are zero based and this method pulls from the first to the 69th element of the list. You will end up to something similar to the first method, except the first word in the list won't be printed.

I'm not sure on the third or fourth methods. To my knowledge, print doesn't take arguments like the ones enclosed in parentheses. If I was going to choose between which one of these would probably work the best, I would say the third method will always work better than the fourth (since as I said, python lists are zero based).

The fifth method will cause an out of bounds error because 70 isn't actually a sub-scripted element in your list of 69 items. Your list only goes from 0 to 69, so this is probably the least effective method (since it causes an error).

I would say of all the methods, either modify 1 delineate your output with spaces, or go with 3 if print actually takes arguments in the form that it is written.

#4
_Metal_

_Metal_

    Newbie

  • Members
  • PipPip
  • 10 posts
Thank you whitey6993!

Your post was really helpfull!

Maybe you have the answer to my second question?^^

Edited by _Metal_, 24 December 2010 - 12:45 PM.


#5
_Metal_

_Metal_

    Newbie

  • Members
  • PipPip
  • 10 posts
please anyone?