1

Help with Python

Ash's Avatar Ash3/20/13 7:18 am
1 emeralds 113 2
3/20/2013 3:59 pm
Ash's Avatar Ash
Hey guys. I've been slack, and have gotten stuck on a question that we were given in my uni class. If anyone could help me with this, it would be highly appreciated if you could show me what the heck it's asking for!!

## # REQUIREMENTS SPEC
##
## # Define a procedure to test the type of each of the given
## # three numeric arguments. If the arguments are all of the
## # same type, print a message describing that type.
## # If the arguments are not all of the same type, convert them
## # all to the dominant (most common) type and print a message
## # giving their new values.
## # We assume that numeric arguments are either integers or
## # floating point.
##
## #---------------------------------------------------------

# TESTCASES-----------------------------------------------
"""


>>> number_type_check(1, 2, 3) # Test 1. normal case - all integers
The three arguments are all of type integer.

>>> number_type_check(1.0, 2.22222, 3.1) # Test 2. normal case - all floating point numbers
The three arguments are all of type float.

>>> number_type_check(1.0, 2.22222, 3) # Test 3. normal case - not all the same - two floats, one int
The three arguments were not of the same type!
The new values are 1.0, 2.22222 and 3.0.

>>> number_type_check(1.0, 1, 1) # Test 4. normal case - not all the same - two ints, one float
The three arguments were not of the same type!
The new values are 1, 1 and 1.

>>> number_type_check(-5, -5, -5) # Test 5. normal case - all the same integer
The three arguments are all of type integer.

"""
#---------------------------------------------------------



## DEVELOP YOUR number_type_check FUNCTION HERE



# TESTING-------------------------------------------------

if __name__ == "__main__":
from doctest import testmod
testmod(verbose=True)

If you're able to just tell me what code I should be using, that would be the best possible outcome.
thanks
Posted by Ash's Avatar
Ash
Retired Moderator
Level 66 : High Grandmaster uwu
3,222

Create an account or sign in to comment.

2

1
03/20/2013 3:59 pm
Level 66 : High Grandmaster uwu
Ash
Ash's Avatar
Thankyou. SO. Much xD
1
03/20/2013 8:10 am
Level 30 : Artisan Pony
Leizazure
Leizazure's Avatar
I am a beginner at Python and decided to tackle on your question and managed to solve it with basic knowledge and the type() function. Probably not the most efficient but it's idiot-proof! Also some funky business with the floating numbers being rounded off to an integer.

a1=input('First value.')
b1=input('Second value.')
c1=input('Third value.')
a=str(type(a1))
b=str(type(b1))
c=str(type(c1))
d=0
e=0
if a == "<type 'int'>":
d=d+1
elif a=="<type 'float'>":
e=e+1
if b == "<type 'int'>":
d=d+1
elif b == "<type 'float'>":
e=e+1
if c == "<type 'int'>":
d=d+1
elif c == "<type 'float'>":
e=e+1
if d-e==3:
print("Integers behold!")
elif d-e==-3:
print("The Floats have won!")
elif d>e:
print(int(a1))
print(int(b1))
print(int(c1))
print("The rebel floats have been brainwashed.")
elif e>d:
a2=a1+0.0
b2=b1+0.0
c2=c1+0.0
print(a2,b2,c2)
print("We've tortured the integers long enough, they renounce their faith.")
Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome