d2jsp
Log InRegister
d2jsp Forums > Off-Topic > General Chat > Homework Help > *300fg For Easy Python Help*
Add Reply New Topic New Poll
Member
Posts: 7,350
Joined: Jun 15 2010
Gold: 95.09
Jan 30 2017 09:40am
Hello, I'm looking for help on how to program with python. Very simple questions, shouldn't take too long to do.
These are the questions:

1. Input three numbers from the user, compute their sum and output the sum to the screen.
For example, to input a value from the keyboard to a variable x, you can use:

x = input (“Please input a number: “)

Import math
Def add (x1 + x2 + x3):
X = input (“Please input a number:”)
X1 = x
X2 = x
X3 = x
Return (x1+x2+x3)
Print(“The value of C = “, c)

2. Input a distance in kilometers from the user, convert it to miles, and output the miledistance.

X = input (“Please input a distance in kilometers:”)
Def convert (x1/x2 = x)
Return (x1/x2 = x)
Print (x + “miles”)


3. Ask the user for their pet’s name, and then tell them how adorable their pet is, saying “<name> is just adorable!”. Where <name> is the name input by theuser.

Print (“What is your pet’s name?)
X = Input (“X”)
Return (“X” is just adorable!”)

>>>name = input('What is your name?: ')
What is your name?: Charles
>>>print('Hello', name)
Hello Charles

4. Input a salary, and compute and output a basic tax on the salary assuch:
a. If the salary is in [0-50000[ , the tax is 15percent.
b. If the salary greater than or equal to 50000, the tax is 25percent.


X = Input (“Please input your salary so that we may calculate the tax:”)
X = (“0 < 50000”)*0.15
X = (“>50000”*0.25
Print (“Your tax is “X1”, “X2”)


5. Input two numbers from the user, swap the two numbers (using a third memory location), and output the numbers to the screen after swapping.

X = Input (“Please input two numbers:”)
def reverse(x):
y1 = x // 10
y2 = x % 10
return y2*10+y1


5. Input a GPA of a student in the range [0,4]. If the GPA isin:
a. [3-4] you say“Superb!”
b. [2-3[ you say“Good!”
c. [1-2[ you say“Hmm!”
d. [0-1[ you say “Nocomment!”

X = (“Please input a GPA from 0-4:”)
X1 = “3-4”
Print (“Superb!”)

X2 = “2-3”
Print (“Good!”)

X3 = “1-2”
Print (“Hmm!”)

X4 = “0-1”
Print (“No comment!”)


6. Input a number from the user and check whether the sum of the digits of the number is divisible by 7. You need to let the user know using an output message.

X = Input (“Please input a number that is divisible by 7:”)
Return (“X/7”)
Print (“The number is “X”, the number was divisible by 7”)
Print (“The number was not divisible by 7, please enter another number:”)


7. Input a phone number from the user in the form: (XXX)XXX-XXXX. If the number is not in this form, you need to give the user an error message, and ask them to try again (only once). Once the user inputs a valid number, you need to extract and print to the screen the area code.

X = Input (“Please input a number including the area code, in a form similar to this (XXX)XXX-XXXX:”)
Print (“The numbers included are not in the right form please try again:”)
Print (“The numbers are in the right form, thank you”)

8. Input someone’s first and last name as one string, and extract andoutput their last name to the screen.
X1 = “X”
X2 = “X”
Print (X1 = X2)

300fg for the first person who helps me with all of the answers (and they're correct)
Member
Posts: 2,404
Joined: Jun 20 2008
Gold: 0.00
Jan 30 2017 10:39am
#1)
import math
def add():
x = input ("Please input a number:")
X1 = float(x)
x = input ("Please input a number:")
X2 = float(x)
x = input ("Please input a number:")
X3 = float(x)
return (X1+X2+X3)
print("The value of C = ", add())

#2)
def convert ():
x = input ("Please input miles:")
x = float(x)
return (x*1.60934)
print ("Kilometer equivalent = ", convert())

#3)

def name():
x=input("Pet name ? ")
return x
print(name() + " is just adorable!")

#4)

def tax():
x=float(input("Salary ? :"))
if x < 50000: return x*0.15
else: return x*0.25

print("Tax ammount: " + str(tax()))


#5)

def swap(a,b):
tmp = a
a = b
b = tmp
return a,b

swap(2,4)

#5.2)

def gpa():
x = float(input("GPA ? "))
if x >=3 and x<=4: return "Superb"
elif x>=2: return "Good"
elif x>=1: return "Hmm!"
elif x>=0: return "No comment"
else: return "Please enter a valid GPA"
gpa()

#6)

def seven():
x = int(input("Input an integer"))
ss = sum(int(k) for k in str(x))
if ss % 7 == 0 : return "sum is divisable by 7"
else: return "Nope"

seven()

#7)

def area():
x=str(input("input phone number in the following form (XXX)XXX-XXXX "))
if not len(x)==13: return "nope"
if not x[0] == ("("): return "nope"
if not x[4] == (")"): return "nope"
if not x[8] == ("-"): return "nope"
ranges=[1,2,3,5,6,7,9,10,11,12]
for w in ranges:
try:
float(x[w])
except Exception:
return "Nope"
return "Valid !"


area()

This post was edited by nignaboutel on Jan 30 2017 10:40am
Go Back To Homework Help Topic List
Add Reply New Topic New Poll