My code:
# Filename: P05_Numbers.py
# Author: Joseph Chua
# Date: 10/24/2016
# Purpose: Evaluating an input and comparing them.
# Other: http://repeatercreeper.me
#========================================================#
# Variables/Input #
#========================================================#
dataType1 = eval(input("Data #1: "), {})
dataType2 = eval(input("Data #2: "), {})
#========================================================#
if type(dataType1) == type(dataType2):
print('''
=============== Data Types ==================
= Data Type #1: %s
= Data Type #2: %s
=============================================
=============== Extra Details ===============
= Data Type Same? => True
= Operation => Addition
= Result => %d
=============================================
''' % (type(dataType1), type(dataType2), (dataType1 + dataType2)))
elif type(dataType1) != type(dataType2):
print('''
=============== Data Types ==================
= Data Type #1: %s
= Data Type #2: %s
=============================================
=============== Extra Details ===============
= Data Type Same? => False
= Operation => Multiplication
= Result => %d
=============================================
''' % (type(dataType1), type(dataType2), (dataType1 * dataType2)))
else:
print("You're doing something wrong!")
My Teacher's code:
# File Name: KEY P5b Numbers # Author: XXXXXXXX # Date: 10/24/2016 # Purpose: To input two different numbers then have Python compare the numbers in a For Loop. #------------------------------------------------------------------------------------------------ for i in range(0,3): num1 = float(input("Please input a Number, ")) if num1 == int(num1): num1 = int(num1) print('This value is an integer!') elif num1 == float(num1): print('This value is a decimal!') else: print('YOUR INPUT WAS NO A NUMBER; Guess again') #The above if, elif, else is to determine whether the input is a number or a letter #If a number then what type? Decimal which is a float or an Integer which is a whole number num2 = float(input("Please input a Number, ")) if num2 == int(num2): num2 = int(num2) print('This value is an integer!') elif num2 == float(num2): print('This value is a decimal! ') else: print('YOUR INOUT WAS NOT A NUMBER; Guess again') #We are now ADDING together the numbers if they are the same data type #Meaning Integers with Integers & Numbers with Numbers if num1 == int(num1) and int(num2): #Comparing values print('both values are integers') num3 = num1 + num2 print(num3) elif num1 == float(num1) and num2 == int(num2): print('first value is a float and the second value is a integer') num3 = num1 * num2 print(num3) elif num1 == int(num1) and num2 == float(num2): print('first value is a integer and the second value is a float') num3 = num1 * num2 print(num3) elif num1 == float(num1) and float(num2): #Comparing Values print('both values are floats') else: print('Your input is not valid.')
So I'm just looking for improvements that can be done with my code.
EXTRA: One of them is to actually be able to detect whether it's a string or not a string without destroying my program. I have tried a try catch valueerror thingy and it still somehow throws something else.
Instructions are as follows:
Activity 1: Write the Python code to allow the input of two numbers and then compare the datatypes.
- a) Within a FOR loop that iterates three (3) times, allow the user to input two numeric values, then compare the values to see if the datatypes are the same (integer or float) within an IF…ELIF…ELSE control structure, printing the datatypes and results.
- Allow the user to input the first numeric value.
- Validate the input as a value.
- Allow the user to input the second numeric value.
- Validate the input as a value.
- Using a control structure (IF...ELIF...ELSE), compare the two numeric values.
- If the values are of the same datatype, print the datatype, add the values together and print the result.
- If the values are of different datatypes, print the datatype of each value, multiple the values together and print the result.
That's the exact same instructions that is written on the docx file.
num1 = float(…)
followed byelif num1 == float(num1)
is silly, because it is converting a float to a float. \$\endgroup\$