user3024631
user3024631

Reputation: 1

Trying to print with spaces instead of line changes

I'm just discovering Python, and I can't find a way to avoid a line change between each word I want to print. I know it must be a often people often ask, and that's why I looked for the answer, but couldn't find anything which works. I've read it once was possible to put a comma at the end of my print command, but it has stopped to work this way since a new version of Pyzo was released. I also read about typing print(bla, sep=" ") but it doesn't work, so basically I don't know what to do. Here's what I typed :

from random import *
def vocabulaire(x):
i=1
while i<=x/2:
    print(choice(français, sep=""))
    i=i+1
while x>=i>x/2:
    print(choice(anglais,sep=""))
    i=i+1

Basically, if I try and execute this programm, Pyzo prints :

"Traceback (most recent call last):
  File "<console>", line 1, in <module>
    from random import *
  File "C:\Users\Sébastien\Desktop\Pyzo_TD1.py", line 86, in vocabulaire
    # The full license can be found in 'license.txt'.
TypeError: choice() got an unexpected keyword argument 'sep' "

The program does work if I just delete the " sep="" ", but not the way I'd like it to.

Do you know what I should do ? (I may have done some grammar mistakes in this post, sorry about that, I'm not fluent in english)

Ps : "français" and "anglais" are two lists

Upvotes: 0

Views: 119

Answers (3)

flyer
flyer

Reputation: 9816

Maybe this is what you want:

import random

def vocabulaire(x, list_one, list_two):
    i = 1
    while i <= x/2:
        print random.choice(list_one),
        i += 1
    while x >= i > x/2:
        print random.choice(list_two),
        i += 1

If you are not sure how to use a function, such as random.choice, you can use ipython to get some help. For example, in the shell, type

$ ipython

then, in ipython, type the following statements:

import random
help(random.choice)

May they will help :)

Upvotes: 0

Luyi Tian
Luyi Tian

Reputation: 371

If you don not want to change line , just add ',' i.e:

print "aaa",
print "bbb"

Upvotes: 0

thefourtheye
thefourtheye

Reputation: 239473

sep is a parameter to print, not to choice. So, code should have been like this

print(choice(français), sep=" ")
...
print(choice(anglais), sep=" ")

If you actually wanted to print them in a single line, you should use end parameter, not the sep parameter, like this

print(choice(français), end=" ")
...
print(choice(anglais), end=" ")

Upvotes: 2

Related Questions