Help me with Python please (picture)

G

Gary Herron

http://imgur.com/E6vrNs4


Can't seem to be getting an output.

Please find a way to include the code in the email. Also what evidence
do you provide that it does not work? What happened when you ran it?
What did you expect to happen? What were you trying to achieve?

Since you are asking volunteers to help, it would be polite to take the
time to explain things carefully.


Gary Herron
 
D

Dave Angel

http://imgur.com/E6vrNs4


Can't seem to be getting an output.

Please compose a text message containing a description of the
environment, the (small) code, and the expected results. This is a text
mailing list, and posting transient images on untrusted websites is
not likely to get you much help.

And when copying the code and the output, please use cut and paste;
don't paraphrase or retype anything.

if you don't know how to capture the text, describe your environment,
and somebody will probably be able to help you.
 
S

Steven D'Aprano

http://imgur.com/E6vrNs4


Can't seem to be getting an output.

The problem with your code has to do with the code between pixel
coordinates (29, 234) and (175, 249) approximately. If you open your
image in a pixel editor, copy and paste that part out, run it through
some OCR software, and then add "print " (including the space) before it,
you will probably get the result you are after.

By the way, posting a picture of your code is thoughtless and rude to
those contributors and programmers who are blind or partially sighted. If
you post text, they can contribute using a screen-reader. If you post a
picture, they're screwed. Even if you don't give two hoots for the blind,
at least consider that maybe they can answer your question when nobody
else can.

Why would we want to follow a link to look at your code, when we could
read it right here in your message? We might not have access to imgur
(many places block it). We cannot copy and paste your code to run it to
see what it does, or make edits to it. If we want to describe parts of
your code we have to re-type it, possibly introducing our own typos.

I really don't understand why you would take program code, which is text,
and take a screenshot of it, then spend time blanking out parts of the
screenshot. Text is *much* more useful for a programmer. It astonishes me
that there is an entire subreddit on imgur dedicated to people who take
screenshots of their code. What's wrong with them?

Could be worse -- at least you didn't take a photo of the screen with
your phone, them email the picture to yourself, then take a screenshot of
the photo in your mail client, then upload that screenshot to imgur, then
take a screenshot of the picture on imgur, then post that screenshot here.
 
C

Chris Angelico

Could be worse -- at least you didn't take a photo of the screen with
your phone, them email the picture to yourself, then take a screenshot of
the photo in your mail client, then upload that screenshot to imgur, then
take a screenshot of the picture on imgur, then post that screenshot here.

http://xkcd.com/763/

ChrisA
 
D

dvghana

http://imgur.com/E6vrNs4





Can't seem to be getting an output.

All the comments about using an image to ask for help over here is extremely valid so I hope you accept it in good faith. I am a noob like you so I can tolerate it and see if I can help you.

So here we go:
1. random.randit will only return an integer but it sounds to me like you are trying to return one of the elements in "chars"

If my understanding is correct try using random.choice instead.

To return a random character from the alphabets you can try:
#string.ascii_uppercase for uppercace

2. you may not need the main() function.
and you didn't have any 'print' statement so when you run the code you won't see anything. You are simply generating random characters and throwing them away

try:
print (random_characters(8))

3. but if you run the code as it stands it won't generate 8 random charaters so you'll actually have to be appending it on a list.

So instead of:
new_string = ''
try:
new_string = []

4. Finally, join the elements in the list once they are generated like this:
return "".join(new_string)
but don't forget to append each character anytime the loop runs this way:
new_string.append(random.choice(string.ascii_lowercase))

Overall I wrote my own version of the code and this is what I got:


******************
import string
import random

def random_characters(number):
i = 0
new_string = []

while (i < number) :
new_string.append(random.choice(string.ascii_lowercase))
i = i + 1
return "".join(new_string)


print(random_characters(3))
*******
 
J

Joel Goldstick

http://imgur.com/E6vrNs4





Can't seem to be getting an output.

All the comments about using an image to ask for help over here is
extremely valid so I hope you accept it in good faith. I am a noob like you
so I can tolerate it and see if I can help you.

So here we go:
1. random.randit will only return an integer but it sounds to me like you
are trying to return one of the elements in "chars"

If my understanding is correct try using random.choice instead.

To return a random character from the alphabets you can try:
#string.ascii_uppercase for uppercace

2. you may not need the main() function.
and you didn't have any 'print' statement so when you run the code you
won't see anything. You are simply generating random characters and
throwing them away

try:
print (random_characters(8))

3. but if you run the code as it stands it won't generate 8 random
charaters so you'll actually have to be appending it on a list.

So instead of:
new_string = ''
try:
new_string = []

4. Finally, join the elements in the list once they are generated like
this:
return "".join(new_string)
but don't forget to append each character anytime the loop runs this way:
new_string.append(random.choice(string.ascii_lowercase))

Overall I wrote my own version of the code and this is what I got:


******************
import string
import random

def random_characters(number):
i = 0
new_string = []

while (i < number) :
new_string.append(random.choice(string.ascii_lowercase))
i = i + 1
return "".join(new_string)


print(random_characters(3))
*******

I'm guessing this is homework since its kind of a contrived exercise. If
you do any reading of python tutorials or books for beginners I don't
think you will see this sort of loop used in python:

while i < number:
do_something ...
i += 1

This is even weirder:

while (i < number):



Its not that you can't do that. But it is much more common to see in other
languages (like C or maybe PHP, others ?). So either the instructor is
promoting this kind of loop because he knows a little about another
language and not so much about python, or the student has some knowledge
of another language.

I prefer:
for i in range(number):
do_something...


and finally

plus + on the calling out of using images to post code. I'm guessing the
OP is a computer user in the sense of running word, or watching you-tube
videos, etc. Most non-programmers never used a plain text editor. If you
want to learn how to write code, and you are a beginner, use the simplest
text editor on your computer, learn to open a shell and run python
interactively and run programs that you saved with your text editors. Be
glad for simplicity. And happy you don't write code on these:
http://en.wikipedia.org/wiki/Hollerith_cards
 
D

Dave Angel

On Saturday, September 28, 2013 12:43:42 AM UTC, (e-mail address removed) wrote:
Overall I wrote my own version of the code and this is what I got:


******************
import string
import random

def random_characters(number):
i = 0
new_string = []

while (i < number) :
new_string.append(random.choice(string.ascii_lowercase))
i = i + 1
return "".join(new_string)


print(random_characters(3))
*******

First, I'd clean up the variable name, and use a for loop instead of a
while loop.

import string
import random

def random_characters(number):
new_list = []
for i in range(number):
new_list.append(random.choice(string.ascii_lowercase))
return "".join(new_list)


print(random_characters(8))

Then I'd probably replace the function body with:

def random_characters(number):
return "".join([random.choice(string.ascii_lowercase) for i in
range(number)])
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,216
Messages
2,571,120
Members
47,722
Latest member
Alexcarry541

Latest Threads

Top