Help please, why doesn't it show the next input?

W

William Bryant

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)
 
J

John Gordon

In said:
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:
 
W

William Bryant

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. -=




def NOS():
if I =3D=3D "String" or "string" or "STRING" or "s" or "S" or "str":

elif I =3D=3D "Number" or "number" or "NUMBER" or "N" or "N" or "int" o=
r "num":


global I
I =3D "You did not enter a valid field, :p Sorry."



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

(e-mail address removed) 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()
 
J

Jugurtha Hadjar

Hello, William


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.
 
D

Dave Angel

<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?")
 
J

John Gordon

In said:
@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:
hello there
 
P

Prasad, Ramit

William said:
Sent: Wednesday, September 11, 2013 2:32 PM
To: (e-mail address removed)
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.
Help on built-in function lower:

lower(...)
S.lower() -> string

Return a copy of the string S converted to lowercase.
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.['__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 andcompleteness of information, viruses, confidentiality, legal privilege, andlegal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email.
 
T

Terry Reedy

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,at interactive prompt prints a page. Learn to use help(ob) also.
 
J

Jugurtha Hadjar

@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()
 
J

Jugurtha Hadjar

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!"
 
W

William Bryant

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.
 
O

Oscar Benjamin

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
 
W

William Bryant

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()
 
J

John Gordon

In said:
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)
 
M

MRAB

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:
 

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

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top