Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #53943 > unrolled thread
| Started by | William Bryant <gogobebe2@gmail.com> |
|---|---|
| First post | 2013-09-10 21:49 -0700 |
| Last post | 2013-09-13 16:34 -0700 |
| Articles | 19 — 8 participants |
Back to article view | Back to comp.lang.python
Help please, why doesn't it show the next input? William Bryant <gogobebe2@gmail.com> - 2013-09-10 21:49 -0700
Re: Help please, why doesn't it show the next input? John Gordon <gordon@panix.com> - 2013-09-11 05:11 +0000
Re: Help please, why doesn't it show the next input? William Bryant <gogobebe2@gmail.com> - 2013-09-10 22:39 -0700
Re: Help please, why doesn't it show the next input? Jugurtha Hadjar <jugurtha.hadjar@gmail.com> - 2013-09-11 10:32 +0100
Re: Help please, why doesn't it show the next input? William Bryant <gogobebe2@gmail.com> - 2013-09-11 12:33 -0700
Re: Help please, why doesn't it show the next input? John Gordon <gordon@panix.com> - 2013-09-11 19:46 +0000
Re: Help please, why doesn't it show the next input? Jugurtha Hadjar <jugurtha.hadjar@gmail.com> - 2013-09-12 02:14 +0100
Re: Help please, why doesn't it show the next input? Jugurtha Hadjar <jugurtha.hadjar@gmail.com> - 2013-09-12 02:08 +0100
Re: Help please, why doesn't it show the next input? Dave Angel <davea@davea.name> - 2013-09-11 11:50 +0000
Re: Help please, why doesn't it show the next input? William Bryant <gogobebe2@gmail.com> - 2013-09-11 12:31 -0700
Re: Help please, why doesn't it show the next input? Dave Angel <davea@davea.name> - 2013-09-11 20:32 +0000
RE: Help please, why doesn't it show the next input? "Prasad, Ramit" <ramit.prasad@jpmorgan.com.dmarc.invalid> - 2013-09-11 19:58 +0000
Re: Help please, why doesn't it show the next input? Terry Reedy <tjreedy@udel.edu> - 2013-09-11 17:14 -0400
Re: Help please, why doesn't it show the next input? William Bryant <gogobebe2@gmail.com> - 2013-09-11 23:04 -0700
Re: Help please, why doesn't it show the next input? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-09-12 10:39 +0100
Re: Help please, why doesn't it show the next input? William Bryant <gogobebe2@gmail.com> - 2013-09-13 15:12 -0700
Re: Help please, why doesn't it show the next input? John Gordon <gordon@panix.com> - 2013-09-13 22:41 +0000
Re: Help please, why doesn't it show the next input? MRAB <python@mrabarnett.plus.com> - 2013-09-13 23:48 +0100
Re: Help please, why doesn't it show the next input? William Bryant <gogobebe2@gmail.com> - 2013-09-13 16:34 -0700
| From | William Bryant <gogobebe2@gmail.com> |
|---|---|
| Date | 2013-09-10 21:49 -0700 |
| Subject | Help please, why doesn't it show the next input? |
| Message-ID | <ef8de6db-5f35-4d07-8306-bcec47b1e69b@googlegroups.com> |
Hey, I am very new to python, I am 13 years old. I want to be able to make a program the caculates the mean, meadian and mode. When i run the program, an input field pops up and says 'Does your list contain, a number or a string?' like I want it to, but when I type in something that is not one of valid field options: "String" or "string" or "STRING" or "s" or "S" or "str" or "Number" or "number" or "NUMBER" or "N" or "N" or "int" or "num", nothing happens, please tell me what I did wrong. Thanks :D Here is my code:
#----- Variables that I am using, including the list. -----#
#False means num
#True means string
num_or_string = ""
amount_numbers = []
List = []
I = "Does your list contain, a number or a string?"
#----- Functions that I am using. -----#
input(I)
def NOS():
if I == "String" or "string" or "STRING" or "s" or "S" or "str":
pass
elif I == "Number" or "number" or "NUMBER" or "N" or "N" or "int" or "num":
pass
else:
global I
I = "You did not enter a valid field, :P Sorry."
input(I)
[toc] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2013-09-11 05:11 +0000 |
| Message-ID | <l0ou1r$c1n$1@reader1.panix.com> |
| In reply to | #53943 |
In <ef8de6db-5f35-4d07-8306-bcec47b1e69b@googlegroups.com> William Bryant <gogobebe2@gmail.com> writes:
> Hey, I am very new to python, I am 13 years old. I want to be able to make =
> a program the caculates the mean, meadian and mode. When i run the program,=
> an input field pops up and says 'Does your list contain, a number or a str=
> ing?' like I want it to, but when I type in something that is not one of va=
> lid field options: "String" or "string" or "STRING" or "s" or "S" or "str" =
> or "Number" or "number" or "NUMBER" or "N" or "N" or "int" or "num", nothin=
> g happens, please tell me what I did wrong. Thanks :D Here is my code:
> #----- Variables that I am using, including the list. --=
> ---#
> #False means num
> #True means string
> num_or_string =3D ""
> amount_numbers =3D []
> List =3D []
> I =3D "Does your list contain, a number or a string?"
> #----- Functions that I am using. -=
> ----#
> input(I)
> def NOS():
> if I =3D=3D "String" or "string" or "STRING" or "s" or "S" or "str":
> pass
> elif I =3D=3D "Number" or "number" or "NUMBER" or "N" or "N" or "int" o=
> r "num":
> pass
> else:
> global I
> I =3D "You did not enter a valid field, :P Sorry."
> input(I)
First of all, you haven't given us your whole program. I know this because
while you've defined the NOS function, it isn't actually called anywhere.
You must have left that part out.
Anyway, I think you're doing two things wrong:
1. You aren't capturing the user's input. The input() function returns
the user's input, but you aren't assigning this to a variable; it's just
being thrown away. You should call input() like this:
user_input = input(I)
(Also, it would probably be clearer to use a name like "prompt" instead
of "I".)
2. You aren't using the compound "if" statement correctly. You can't
do this (well, you CAN, but it won't do what you want):
if a == 1 or 2 or 3:
You should fully spell out each condition, like this:
if a == 1 or a == 2 or a == 3:
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | William Bryant <gogobebe2@gmail.com> |
|---|---|
| Date | 2013-09-10 22:39 -0700 |
| Message-ID | <2dba1d63-16f0-412d-b650-0cdc27c3b57e@googlegroups.com> |
| In reply to | #53944 |
On Wednesday, September 11, 2013 5:11:23 PM UTC+12, John Gordon wrote:
> In <ef8de6db-5f35-4d07-8306-bcec47b1e69b@googlegroups.com> William Bryant <gogobebe2@gmail.com> writes:
>
>
>
> > Hey, I am very new to python, I am 13 years old. I want to be able to make =
>
> > a program the caculates the mean, meadian and mode. When i run the program,=
>
> > an input field pops up and says 'Does your list contain, a number or a str=
>
> > ing?' like I want it to, but when I type in something that is not one of va=
>
> > lid field options: "String" or "string" or "STRING" or "s" or "S" or "str" =
>
> > or "Number" or "number" or "NUMBER" or "N" or "N" or "int" or "num", nothin=
>
> > g happens, please tell me what I did wrong. Thanks :D Here is my code:
>
>
>
> > #----- Variables that I am using, including the list. --=
>
> > ---#
>
>
>
> > #False means num
>
> > #True means string
>
> > num_or_string =3D ""
>
> > amount_numbers =3D []
>
> > List =3D []
>
> > I =3D "Does your list contain, a number or a string?"
>
>
>
> > #----- Functions that I am using. -=
>
> > ----#
>
> > input(I)
>
>
>
> > def NOS():
>
> > if I =3D=3D "String" or "string" or "STRING" or "s" or "S" or "str":
>
> > pass
>
> > elif I =3D=3D "Number" or "number" or "NUMBER" or "N" or "N" or "int" o=
>
> > r "num":
>
> > pass
>
> > else:
>
> > global I
>
> > I =3D "You did not enter a valid field, :P Sorry."
>
> > input(I)
>
>
>
> First of all, you haven't given us your whole program. I know this because
>
> while you've defined the NOS function, it isn't actually called anywhere.
>
> You must have left that part out.
>
>
>
> Anyway, I think you're doing two things wrong:
>
>
>
> 1. You aren't capturing the user's input. The input() function returns
>
> the user's input, but you aren't assigning this to a variable; it's just
>
> being thrown away. You should call input() like this:
>
>
>
> user_input = input(I)
>
>
>
> (Also, it would probably be clearer to use a name like "prompt" instead
>
> of "I".)
>
>
>
> 2. You aren't using the compound "if" statement correctly. You can't
>
> do this (well, you CAN, but it won't do what you want):
>
>
>
> if a == 1 or 2 or 3:
>
>
>
> You should fully spell out each condition, like this:
>
>
>
> if a == 1 or a == 2 or a == 3:
>
>
>
> --
>
> John Gordon A is for Amy, who fell down the stairs
>
> gordon@panix.com B is for Basil, assaulted by bears
>
> -- Edward Gorey, "The Gashlycrumb Tinies"
Thanks so much for putting your time into helping me!! It worked! Could you tell me if I did anything wrong this time? :)
#----- Variables that I am using, including the list. -----#
num_or_string = ""
amount_numbers = []
List = []
prompt = "Does your list contain, a number or a string?"
#----- Functions that I am using. -----#
user_input1 = input(prompt)
user_input1
def NOS():
if user_input1 == "String" or user_input1 == "string" or user_input1 == "STRING" or user_input1 == "s" or user_input1 == "S" or user_input1 == "str":
print("a")
elif user_input1 == "Number" or user_input1 == "number" or user_input1 == "NUMBER" or user_input1 == "N" or user_input1 == "N" or user_input1 == "int" or user_input1 == "num":
print("a")
else:
global user_input2
global prompt
prompt = "You did not enter a valid field, :P Sorry."
user_input2 = input(prompt)
user_input2
NOS2()
def NOS2():
if user_input2 == "String" or user_input2 == "string" or user_input2 == "STRING" or user_input2 == "s" or user_input2 == "S" or user_input2 == "str":
print("a")
elif user_input2 == "Number" or user_input2 == "number" or user_input2 == "NUMBER" or user_input2 == "N" or user_input2 == "N" or user_input2 == "int" or user_input2 == "num":
print("a")
else:
global user_input2
global prompt
prompt = "You did not enter a valid field, :P Sorry."
user_input2 = input(prompt)
user_input2
NOS2()
NOS()
[toc] | [prev] | [next] | [standalone]
| From | Jugurtha Hadjar <jugurtha.hadjar@gmail.com> |
|---|---|
| Date | 2013-09-11 10:32 +0100 |
| Message-ID | <mailman.239.1378898124.5461.python-list@python.org> |
| In reply to | #53945 |
Hello, William
On 09/11/2013 06:39 AM, William Bryant wrote:
> user_input1 = input(prompt)
I take it you are using Python 3.x . If that's not the case, take a look
at raw_input().
> def NOS():
> if user_input1 == "String" or user_input1 == "string" or user_input1 == "STRING" or user_input1 == "s" or user_input1 == "S" or user_input1 == "str":
> print("a")
What if you create a list containing these, and then you check if the
user_input is contained in that list. But before that, get the
lower-case of it so you don't deal with String sTring string, s, S, etc...
Something like this
You add this to the variables you are using section
string_list = ['string', 's']
When you want to check the check
if user_input.lower() in string_list:
{your_code_here}
> elif user_input1 == "Number" or user_input1 == "number" or user_input1 == "NUMBER" or user_input1 == "N" or user_input1 == "N" or user_input1 == "int" or user_input1 == "num":
> print("a")
Same goes with that
But some questions:
1 - Why does the user have to specify that, why doesn't the program
itself tell if it is a string or a number ? You just give it the data
and it's up to him to tell.
2 - What is it you are trying to accomplish: Maybe there are shortcuts
or better ways to do things.
--
~Jugurtha Hadjar,
[toc] | [prev] | [next] | [standalone]
| From | William Bryant <gogobebe2@gmail.com> |
|---|---|
| Date | 2013-09-11 12:33 -0700 |
| Message-ID | <f7db6733-0f7a-4b18-bdcd-8d53e3644b50@googlegroups.com> |
| In reply to | #53950 |
@Jugurtha Hadjar What does user_input.lower() mean/do?
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2013-09-11 19:46 +0000 |
| Message-ID | <l0qhbc$nh7$1@reader1.panix.com> |
| In reply to | #53986 |
In <f7db6733-0f7a-4b18-bdcd-8d53e3644b50@googlegroups.com> William Bryant <gogobebe2@gmail.com> writes:
> @Jugurtha Hadjar
> What does user_input.lower() mean/do?
String objects have a number of built-in functions, lower() being one of
them. It returns a copy of the string with all uppercase letters converted
to lowercase.
Example:
>>> x = "Hello There"
>>> y = x.lower()
>>> print y
hello there
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | Jugurtha Hadjar <jugurtha.hadjar@gmail.com> |
|---|---|
| Date | 2013-09-12 02:14 +0100 |
| Message-ID | <mailman.294.1378950534.5461.python-list@python.org> |
| In reply to | #53986 |
On 09/12/2013 02:08 AM, Jugurtha Hadjar wrote: > Try this on your python prompt > > mystring = "ThIs Is ThE wAy SoMe StUpId PeOpLe WrItE i DoN't KnOw WhY!" > mystring.lower() > This should return: "You shouldn't treat people of stupid, but I feel your pain", or let's be more realistic: "this is the way some stupid people write i don't know why!" -- ~Jugurtha Hadjar,
[toc] | [prev] | [next] | [standalone]
| From | Jugurtha Hadjar <jugurtha.hadjar@gmail.com> |
|---|---|
| Date | 2013-09-12 02:08 +0100 |
| Message-ID | <mailman.293.1378950534.5461.python-list@python.org> |
| In reply to | #53986 |
On 09/11/2013 08:33 PM, William Bryant wrote: > @Jugurtha Hadjar > What does user_input.lower() mean/do? > Hello, As did other people point out, it returns the lower case of a string. It's not user_input.lower(), it's any_string.lower() For example: Try this on your python prompt mystring = "ThIs Is ThE wAy SoMe StUpId PeOpLe WrItE i DoN't KnOw WhY!" mystring.lower() -- ~Jugurtha Hadjar,
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-09-11 11:50 +0000 |
| Message-ID | <mailman.242.1378900277.5461.python-list@python.org> |
| In reply to | #53945 |
On 11/9/2013 01:39, William Bryant wrote:
> On Wednesday, September 11, 2013 5:11:23 PM UTC+12, John Gordon wrote:
>> In <ef8de6db-5f35-4d07-8306-bcec47b1e69b@googlegroups.com> William Bryant <gogobebe2@gmail.com> writes:
>>
>>
>>
>> > Hey, I am very new to python, I am 13 years old. I want to be able to make =
>>
>> > a program the caculates the mean, meadian and mode. When i run the program,=
>>
>> > an input field pops up and says 'Does your list contain, a number or a str=
>>
>> > ing?' like I want it to, but when I type in something that is not one of va=
>>
>> > lid field options: "String" or "string" or "STRING" or "s" or "S" or "str" =
<etc.>
Thanks for quoting some context. However, since you're using buggy
googlegroups, please either constrain your quoting to a couple of lines,
or fix its display to NOT doublespace everything.
See http://wiki.python.org/moin/GoogleGroupsPython
>
> Thanks so much for putting your time into helping me!! It worked! Could you tell me if I did anything wrong this time? :)
>
> #----- Variables that I am using, including the list. -----#
>
> num_or_string = ""
> amount_numbers = []
> List = []
> prompt = "Does your list contain, a number or a string?"
>
> #----- Functions that I am using. -----#
> user_input1 = input(prompt)
> user_input1
This last line does nothing useful. You may be confused because in the
debugger, a lone expression getes its value printed. But that's a
debugger feature not a language one. If you want to see what got
input, you'd want to use print() function.
Looks like the second function is identical to the first. if I'm right,
then just change the NOS2 into NOS1, and save lots of redundancy.
>
> def NOS():
> if user_input1 == "String" or user_input1 == "string" or user_input1 == "STRING" or user_input1 == "s" or user_input1 == "S" or user_input1 == "str":
> print("a")
> elif user_input1 == "Number" or user_input1 == "number" or user_input1 == "NUMBER" or user_input1 == "N" or user_input1 == "N" or user_input1 == "int" or user_input1 == "num":
> print("a")
> else:
> global user_input2
> global prompt
> prompt = "You did not enter a valid field, :P Sorry."
> user_input2 = input(prompt)
> user_input2
> NOS2()
Here you're using the recursive call to NOS() to substitute for a loop.
Recursion is very useful, but in this case, it hides what's really
happening, and it prevents you from writing a conventional function with
parameters and return value.
I don't know what NOS is supposed to stand for, but it's generally
useful if a function does a simply described single thing. In this
case, it gets input from the user, and asks the user repeatedly till
he/she gets it right. So the function should do its own input, and not
expect the top-level code to already have asked once.
Something like:
SOMETHING = [ .... list of the valid inputs ]
def query_user(prompt):
while true:
udata = input(prompt)
if udata.lower() in SOMETHING
return udata
print("Invalid field, sorry")
And now you call this function the same way you would have called
input() function:
user_input1 = query_user("Does your list contain, a number or a
string?")
>
> NOS()
--
DaveA
[toc] | [prev] | [next] | [standalone]
| From | William Bryant <gogobebe2@gmail.com> |
|---|---|
| Date | 2013-09-11 12:31 -0700 |
| Message-ID | <b4c3b51d-8e10-4fff-8b36-a34dd5cc9af4@googlegroups.com> |
| In reply to | #53953 |
@Dave Angel What is .lower() ?
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-09-11 20:32 +0000 |
| Message-ID | <mailman.264.1378931564.5461.python-list@python.org> |
| In reply to | #53985 |
On 11/9/2013 15:31, William Bryant wrote:
> @Dave Angel
>
> What is .lower() ?
It is a method on the str class.
You could teach yourself. At the interpreter prompt, type
>>>> help("test response".lower)
Or on the web:
http://docs.python.org/2/library/stdtypes.html#str.lower
There are lots of other interesting methods in the str class, listed on
that section of that page.
--
DaveA
[toc] | [prev] | [next] | [standalone]
| From | "Prasad, Ramit" <ramit.prasad@jpmorgan.com.dmarc.invalid> |
|---|---|
| Date | 2013-09-11 19:58 +0000 |
| Message-ID | <mailman.265.1378932066.5461.python-list@python.org> |
| In reply to | #53985 |
William Bryant wrote:
> Sent: Wednesday, September 11, 2013 2:32 PM
> To: python-list@python.org
> Subject: Re: Help please, why doesn't it show the next input?
>
> @Dave Angel
>
> What is .lower() ?
Thanks for bottom posting and trimming, but you should
leave some content quoted for context. Otherwise
people will not what you are referring to.
You can find answers to questions like this in the API.
http://docs.python.org/2/library/stdtypes.html#str.lower
I would recommend taking time to get familiar with the
Athena interpreter. It can also answer questions like
this (but with less detail than the web API).
You can use dir() to find the attributes on any object.
help() will give you docstring for the attribute/class.
>>> 'A'.lower()
'a'
>>> help(''.lower)
Help on built-in function lower:
lower(...)
S.lower() -> string
Return a copy of the string S converted to lowercase.
>>> help(dir)
Help on built-in function dir in module __builtin__:
dir(...)
dir([object]) -> list of strings
If called without an argument, return the names in the current scope.
Else, return an alphabetized list of names comprising (some of) the attributes
of the given object, and of attributes reachable from it.
If the object supplies a method named __dir__, it will be used; otherwise
the default dir() logic is used and returns:
for a module object: the module's attributes.
for a class object: its attributes, and recursively the attributes
of its bases.
for any other object: its attributes, its class's attributes, and
recursively the attributes of its class's base classes.
>>> dir('')
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem_\
_', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__\
', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclass\
hook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expan\
dtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lo\
wer', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'starts\
with', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
If you want to see all the relevant information you can use help()
on the class [e.g. help(str)]. I think help(class) will not show
functions that start with _ unless they are python magic dunder
functions (e.g. __str__).
~Ramit
This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email.
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2013-09-11 17:14 -0400 |
| Message-ID | <mailman.268.1378934107.5461.python-list@python.org> |
| In reply to | #53985 |
On 9/11/2013 3:31 PM, William Bryant wrote: > What is .lower() ? The Python docs have a pretty good index that includes 'lower() (str method)'. Learn to use it. If you know that .lower is a str method, >>> help(str.lower) at interactive prompt prints a page. Learn to use help(ob) also. -- Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | William Bryant <gogobebe2@gmail.com> |
|---|---|
| Date | 2013-09-11 23:04 -0700 |
| Message-ID | <75200616-79f5-4088-a967-d3a1381716f2@googlegroups.com> |
| In reply to | #53943 |
Thanks everyone for helping but I did listen to you :3 Sorry. This is my code, it works, I know it's not the best way to do it and it's the long way round but it is one of my first programs ever and I'm happy with it:
'''#*************************************************************************'''
#* Name: Mode-Median-Mean Calculator *#
#* *#
#* Purpose: To calculate the mode, median and mean of a list of numbers *#
#* and the mode of a list of strings because that is what we are *#
#* learning in math atm in school :P *#
#* *#
#* Author: William Bryant *#
#* *#
#* Created: 11/09/2013 *#
#* *#
#* Copyright: (c) William 2013 *#
#* *#
#* Licence: IDK :3 *#
'''**************************************************************************'''
#-----# ~~Import things I am using~~ #-----#
# |
# |
# \/
import time
import itertools
#-----# ~~Variables that I am using, including the list.~~ #-----#
# |
# |
# \/
num_or_string = None
amount_numbers = []
List = []
prompt = "Does your list contain, a number or a string? \nEnter: "
user_inputHMNn = None
user_inputHMNs = None
#-----# ~~Functions that I am using.~~ #-----#
# |
# |
# \/
user_inputNOS = input(prompt)
user_inputNOS
time.sleep(1.5)
def NOS():
if user_inputNOS == "String" or user_inputNOS == "string" or user_inputNOS == "STRING" or user_inputNOS == "s" or user_inputNOS == "S" or user_inputNOS == "str":
HMNs()
elif user_inputNOS == "Number" or user_inputNOS == "number" or user_inputNOS == "NUMBER" or user_inputNOS == "N" or user_inputNOS == "N" or user_inputNOS == "int" or user_inputNOS == "num":
HMNn()
else:
global user_inputNOS2
global prompt
prompt = "You did not enter a valid field, :P Sorry. \nEnter: "
user_inputNOS2 = input(prompt)
user_inputNOS2
time.sleep(1.5)
NOS2()
def NOS2():
if user_inputNOS2 == "String" or user_inputNOS2 == "string" or user_inputNOS2 == "STRING" or user_inputNOS2 == "s" or user_inputNOS2 == "S" or user_inputNOS2 == "str":
HMNs()
elif user_inputNOS2 == "Number" or user_inputNOS2 == "number" or user_inputNOS2 == "NUMBER" or user_inputNOS2 == "N" or user_inputNOS2 == "N" or user_inputNOS2 == "int" or user_inputNOS2 == "num":
HMNn()
else:
global prompt
prompt = "You did not enter a valid field, :P Sorry. \nEnter: "
user_inputNOS2
time.sleep(1.5)
NOS2()
def HMNs():
global TheStr, user_inputHMNs, List_input
user_inputHMNs = input("You picked string. This program cannot calculate the mean or median, but it can calculate the mode. :D How many strings are you using in your list? (Can not be a decimal number) \nEnter: ")
user_inputHMNs
time.sleep(1.5)
TheStr = int(user_inputHMNs)
for i in range(TheStr):
List_input = input("Enter your strings. (One in each input field): ")
List.append(List_input)
print("Your list -> ", List)
def HMNn():
global TheNum, user_inputHMNn, List_input
user_inputHMNn = input("You picked number. :D How many numbers are you using in your list? (Can not be a decimal number) \nEnter: ")
user_inputHMNn
time.sleep(1.5)
TheNum = int(user_inputHMNn)
for i in range(TheNum):
List_input = input("Enter your numbers. (One in each input field): ")
List.append(List_input)
print("Your list -> ", List)
#-----# ~~The functions which need calling~~ #-----#
# |
# |
# \/
NOS()
Not done. I'll finish it today! :) Oh and thanks so much for helping, you guys are really nice :D Thanks a lot.
[toc] | [prev] | [next] | [standalone]
| From | Oscar Benjamin <oscar.j.benjamin@gmail.com> |
|---|---|
| Date | 2013-09-12 10:39 +0100 |
| Message-ID | <mailman.304.1378978797.5461.python-list@python.org> |
| In reply to | #54036 |
On 12 September 2013 07:04, William Bryant <gogobebe2@gmail.com> wrote:
> Thanks everyone for helping but I did listen to you :3 Sorry. This is my code, it works, I know it's not the best way to do it and it's the long way round but it is one of my first programs ever and I'm happy with it:
Hi William, I'm glad you've solved your initial problem and I just
wanted to make a couple of comments about how your program could be
simplified or improved. The comments are below.
>
> '''#*************************************************************************'''
> #* Name: Mode-Median-Mean Calculator *#
> #* *#
> #* Purpose: To calculate the mode, median and mean of a list of numbers *#
> #* and the mode of a list of strings because that is what we are *#
> #* learning in math atm in school :P *#
> #* *#
> #* Author: William Bryant *#
> #* *#
> #* Created: 11/09/2013 *#
> #* *#
> #* Copyright: (c) William 2013 *#
> #* *#
> #* Licence: IDK :3 *#
> '''**************************************************************************'''
>
>
>
>
> #-----# ~~Import things I am using~~ #-----#
>
> # |
> # |
> # \/
>
> import time
> import itertools
>
>
>
> #-----# ~~Variables that I am using, including the list.~~ #-----#
>
> # |
> # |
> # \/
>
> num_or_string = None
> amount_numbers = []
> List = []
> prompt = "Does your list contain, a number or a string? \nEnter: "
> user_inputHMNn = None
> user_inputHMNs = None
Of the declarations above only List and prompt are needed. Actually
though I wouldn't use either of those. List should really be a local
variable inside the functions below. And it would be better to write
prompt inline e.g.:
answer = input("Does your list contain, a number or a string? \nEnter: ")
That makes it easier to understand what's happening when you look at
the code. So I would remove all of those variable declarations. In
some other programming languages there are reasons to use declarations
like those above but not in Python.
>
> #-----# ~~Functions that I am using.~~ #-----#
>
> # |
> # |
> # \/
>
> user_inputNOS = input(prompt)
> user_inputNOS
> time.sleep(1.5)
>
> def NOS():
> if user_inputNOS == "String" or user_inputNOS == "string" or user_inputNOS == "STRING" or user_inputNOS == "s" or user_inputNOS == "S" or user_inputNOS == "str":
> HMNs()
> elif user_inputNOS == "Number" or user_inputNOS == "number" or user_inputNOS == "NUMBER" or user_inputNOS == "N" or user_inputNOS == "N" or user_inputNOS == "int" or user_inputNOS == "num":
> HMNn()
> else:
> global user_inputNOS2
> global prompt
> prompt = "You did not enter a valid field, :P Sorry. \nEnter: "
> user_inputNOS2 = input(prompt)
> user_inputNOS2
> time.sleep(1.5)
> NOS2()
>
> def NOS2():
> if user_inputNOS2 == "String" or user_inputNOS2 == "string" or user_inputNOS2 == "STRING" or user_inputNOS2 == "s" or user_inputNOS2 == "S" or user_inputNOS2 == "str":
> HMNs()
> elif user_inputNOS2 == "Number" or user_inputNOS2 == "number" or user_inputNOS2 == "NUMBER" or user_inputNOS2 == "N" or user_inputNOS2 == "N" or user_inputNOS2 == "int" or user_inputNOS2 == "num":
> HMNn()
> else:
> global prompt
> prompt = "You did not enter a valid field, :P Sorry. \nEnter: "
> user_inputNOS2
> time.sleep(1.5)
> NOS2()
The functions above are almost identical. The main difference is just
that NOS2() doesn't call input() which I assume is accidental. So you
could simplify this by only having one function NOS() and having it
call itself rather than NOS2() at the end. Then it would look like:
def NOS():
# code here ...
NOS()
However this is not a common way to use recursive function calls. What
this really does is repeat something forever in a loop. The common way
to do this is to use a for-loop or a while-loop. For example you can
get the same effect with:
while True: # Loops forever (until the break)
answer = input("Does your list contain, a number or a string? \nEnter: ")
answer = answer.lower()
if answer in ("string", "str", "s"):
HMNs()
elif answer in ("number", "num", "n", "int"):
HMNn()
elif answer in ("quit", "q"):
break # Exits the while loop
else:
print("You did not enter a valid field, :P Sorry.")
time.sleep(1.5)
You can put this code in a function or otherwise just move it below
the functions it calls (i.e. HMNs and HMNn) since code doesn't need to
be in functions in a Python script.
> def HMNs():
> global TheStr, user_inputHMNs, List_input
> user_inputHMNs = input("You picked string. This program cannot calculate the mean or median, but it can calculate the mode. :D How many strings are you using in your list? (Can not be a decimal number) \nEnter: ")
> user_inputHMNs
> time.sleep(1.5)
> TheStr = int(user_inputHMNs)
> for i in range(TheStr):
> List_input = input("Enter your strings. (One in each input field): ")
> List.append(List_input)
> print("Your list -> ", List)
>
> def HMNn():
> global TheNum, user_inputHMNn, List_input
> user_inputHMNn = input("You picked number. :D How many numbers are you using in your list? (Can not be a decimal number) \nEnter: ")
> user_inputHMNn
> time.sleep(1.5)
> TheNum = int(user_inputHMNn)
> for i in range(TheNum):
> List_input = input("Enter your numbers. (One in each input field): ")
> List.append(List_input)
> print("Your list -> ", List)
Using global variables in the functions above is confusing and
error-prone. The simpler and safer way to do it is to use local
variables in each function. To do this remove the global statements
and assign an empty list to the name List inside the function like
this:
def HMNn():
List = []
user_inputHMNn = ...
# more code
Then you don't need to declare List at the top of your script.
Oscar
[toc] | [prev] | [next] | [standalone]
| From | William Bryant <gogobebe2@gmail.com> |
|---|---|
| Date | 2013-09-13 15:12 -0700 |
| Message-ID | <364bcdb3-fdd5-4774-b7d2-040e2ccb4cfd@googlegroups.com> |
| In reply to | #54043 |
On Thursday, September 12, 2013 9:39:33 PM UTC+12, Oscar Benjamin wrote:
> On 12 September 2013 07:04, William Bryant <gogobebe2@gmail.com> wrote:
>
> > Thanks everyone for helping but I did listen to you :3 Sorry. This is my code, it works, I know it's not the best way to do it and it's the long way round but it is one of my first programs ever and I'm happy with it:
>
>
>
> Hi William, I'm glad you've solved your initial problem and I just
>
> wanted to make a couple of comments about how your program could be
>
> simplified or improved. The comments are below.
>
Hello, I've done this so far but why doesn't the mode function work?
'''#*************************************************************************'''
#* Name: Mode-Median-Mean Calculator *#
#* *#
#* Purpose: To calculate the mode, median and mean of a list of numbers *#
#* and the mode of a list of strings because that is what we are *#
#* learning in math atm in school :P *#
#* *#
#* Author: William Bryant *#
#* *#
#* Created: 11/09/2013 *#
#* *#
#* Copyright: (c) William 2013 *#
#* *#
#* Licence: IDK :3 *#
'''**************************************************************************'''
#-----# ~~Import things I am using~~ #-----#
# |
# |
# \/
import time
import itertools
#-----# ~~Variables that I am using, including the list.~~ #-----#
# |
# |
# \/
List = []
NumberOfXItems = []
Themode = []
#-----# ~~Functions that I am using.~~ #-----#
# |
# |
# \/
def HMNs():
global TheStr, user_inputHMNs, List_input, List
user_inputHMNs = input("You picked string. This program cannot calculate the mean or median, but it can calculate the mode. :D How many strings are you using in your list? (Can not be a decimal number) \nEnter: ")
user_inputHMNs
time.sleep(1.5)
TheStr = int(user_inputHMNs)
for i in range(TheStr):
List_input = input("Enter your strings. (One in each input field): ")
List.append(List_input)
print("Your list -> ", List)
if List.count == int(user_inputHMNs):
break
mode()
def HMNn():
global TheNum, user_inputHMNn, List_input, List
user_inputHMNn = input("You picked number. :D How many numbers are you using in your list? (Can not be a decimal number) \nEnter: ")
user_inputHMNn
time.sleep(1.5)
TheNum = int(user_inputHMNn)
for i in range(TheNum):
List_input = input("Enter your numbers. (One in each input field): ")
List_input = int(List_input)
List.append(List_input)
print("Your list -> ", List)
if List.count == int(user_inputHMNn):
break
mode()
def NOS():
while True: # Loops forever (until the break)
answer = input("Does your list contain a number or a string? \nEnter: ")
answer = answer.lower()
if answer in ("string", "str", "s"):
HMNs()
break
elif answer in ("number", "num", "n", "int"):
HMNn()
break
elif answer in ("quit", "q"):
break # Exits the while loop
else:
print("You did not enter a valid field, :P Sorry. \nEnter: ")
time.sleep(1.5)
def mode():
global NumberOfXItems, Themode
for i in List:
NumberOfXItems.append(i)
NumberOfXItems.append(List.count(i))
Themode = max(NumberOfXItems)
print(Themode)
#-----# ~~The functions which need calling~~ #-----#
# |
# |
# \/
NOS()
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2013-09-13 22:41 +0000 |
| Message-ID | <l104ab$auc$1@reader1.panix.com> |
| In reply to | #54141 |
In <364bcdb3-fdd5-4774-b7d2-040e2ccb4cfd@googlegroups.com> William Bryant <gogobebe2@gmail.com> writes:
> Hello, I've done this so far but why doesn't the mode function work?
> def mode():
> global NumberOfXItems, Themode
> for i in List:
> NumberOfXItems.append(i)
> NumberOfXItems.append(List.count(i))
> Themode = max(NumberOfXItems)
> print(Themode)
As far as I can see, you're appending each of the user's numbers onto
NumberOfXItems, and you're also appending the number of times each number
occurs.
So, if the user had entered these numbers:
5 9 9 9 15 100 100
NumberOfXItems would end up looking like this:
5 1 9 3 9 3 9 3 15 1 100 2 100 2
The max is 100, but 9 is the most often-occuring number.
Also, since NumberOfXItems mixes user input and the counts of that input,
you risk getting a max that the user didn't even enter. For example if the
user entered these numbers:
1 1 1 1 1 1 2 3
NumberOfXItems would end up looking like this:
1 6 1 6 1 6 1 6 1 6 1 6 2 1 3 1
The max is 6, which is a count, not user input.
mode would be much better written like this:
def mode(mylist):
max_occurrences = 0
themode = None
for i in mylist:
thecount = mylist.count(i)
if thecount > max_occurrences:
max_occurrences = thecount
themode = i
print(themode)
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2013-09-13 23:48 +0100 |
| Message-ID | <mailman.365.1379112525.5461.python-list@python.org> |
| In reply to | #54141 |
On 13/09/2013 23:12, William Bryant wrote:
> On Thursday, September 12, 2013 9:39:33 PM UTC+12, Oscar Benjamin wrote:
>> On 12 September 2013 07:04, William Bryant <gogobebe2@gmail.com> wrote:
>>
>> > Thanks everyone for helping but I did listen to you :3 Sorry. This is my code, it works, I know it's not the best way to do it and it's the long way round but it is one of my first programs ever and I'm happy with it:
>>
>>
>>
>> Hi William, I'm glad you've solved your initial problem and I just
>>
>> wanted to make a couple of comments about how your program could be
>>
>> simplified or improved. The comments are below.
>>
>
> Hello, I've done this so far but why doesn't the mode function work?
>
> '''#*************************************************************************'''
> #* Name: Mode-Median-Mean Calculator *#
> #* *#
> #* Purpose: To calculate the mode, median and mean of a list of numbers *#
> #* and the mode of a list of strings because that is what we are *#
> #* learning in math atm in school :P *#
> #* *#
> #* Author: William Bryant *#
> #* *#
> #* Created: 11/09/2013 *#
> #* *#
> #* Copyright: (c) William 2013 *#
> #* *#
> #* Licence: IDK :3 *#
> '''**************************************************************************'''
>
>
>
>
> #-----# ~~Import things I am using~~ #-----#
>
> # |
> # |
> # \/
>
> import time
> import itertools
>
>
>
> #-----# ~~Variables that I am using, including the list.~~ #-----#
>
> # |
> # |
> # \/
>
Global variables and no parameter passing: yuck! :-)
> List = []
> NumberOfXItems = []
> Themode = []
>
> #-----# ~~Functions that I am using.~~ #-----#
>
> # |
> # |
> # \/
>
Your function names aren't meaningful.
> def HMNs():
> global TheStr, user_inputHMNs, List_input, List
> user_inputHMNs = input("You picked string. This program cannot calculate the mean or median, but it can calculate the mode. :D How many strings are you using in your list? (Can not be a decimal number) \nEnter: ")
This line doesn't do anything:
> user_inputHMNs
> time.sleep(1.5)
This variable is an integer, yet it's called 'TheStr'.
> TheStr = int(user_inputHMNs)
> for i in range(TheStr):
> List_input = input("Enter your strings. (One in each input field): ")
> List.append(List_input)
> print("Your list -> ", List)
Here you're comparing the list's .count method with an integer. It'll
never be true!
> if List.count == int(user_inputHMNs):
> break
> mode()
>
> def HMNn():
> global TheNum, user_inputHMNn, List_input, List
> user_inputHMNn = input("You picked number. :D How many numbers are you using in your list? (Can not be a decimal number) \nEnter: ")
> user_inputHMNn
> time.sleep(1.5)
> TheNum = int(user_inputHMNn)
> for i in range(TheNum):
> List_input = input("Enter your numbers. (One in each input field): ")
> List_input = int(List_input)
> List.append(List_input)
> print("Your list -> ", List)
The same bug as above:
> if List.count == int(user_inputHMNn):
> break
> mode()
> def NOS():
> while True: # Loops forever (until the break)
> answer = input("Does your list contain a number or a string? \nEnter: ")
> answer = answer.lower()
> if answer in ("string", "str", "s"):
> HMNs()
> break
> elif answer in ("number", "num", "n", "int"):
> HMNn()
> break
> elif answer in ("quit", "q"):
> break # Exits the while loop
> else:
> print("You did not enter a valid field, :P Sorry. \nEnter: ")
> time.sleep(1.5)
>
> def mode():
> global NumberOfXItems, Themode
> for i in List:
Here you're appending an item and then the number of times that the item
occurs:
> NumberOfXItems.append(i)
> NumberOfXItems.append(List.count(i))
Here you're getting the maximum entry, be it an item or the number of
times an item occurs (see above). Have a look at the Counter class from
the collections module:
> Themode = max(NumberOfXItems)
> print(Themode)
>
>
>
> #-----# ~~The functions which need calling~~ #-----#
>
> # |
> # |
> # \/
>
> NOS()
>
[toc] | [prev] | [next] | [standalone]
| From | William Bryant <gogobebe2@gmail.com> |
|---|---|
| Date | 2013-09-13 16:34 -0700 |
| Message-ID | <0b2215c0-c496-4df8-a5c1-27ce3632365c@googlegroups.com> |
| In reply to | #54143 |
Thanks for the contructive critisism - :D I'll try fix it up!
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web