d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > *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:41am
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 to the first person that helps with all of the answers (and they're correct)
Member
Posts: 36,389
Joined: Jul 18 2008
Gold: 3,192.00
Jan 30 2017 06:31pm
Part 1

1)

Code
a = input("Please input a number: ")
b = input("Please input a number: ")
c = input("Please input a number: ")
print(str(a + b + c))


2)

Code
a = input("Please input distance in km: ")
print(a / 1.609)


3)

Code
name = raw_input("What is your pet's name? ")
print(name + " is just adorable!")



4)

Code
sal = input("Salary: ")
if (sal < 50000):
print("Tax: " + str((sal*.15)))
else:
print("Tax: " + str((sal*.25)))



5)

Code
gpa = input("GPA? ")

if (gpa > 3 and gpa <= 4):
print("Superb!")
elif (gpa > 2 and gpa <= 3):
print("Good!")
elif (gpa > 1 and gpa <= 2):
print("Hmm!")
else:
print("No comment!")


Member
Posts: 36,389
Joined: Jul 18 2008
Gold: 3,192.00
Jan 30 2017 06:32pm
Part 2

6)

Code
x = input("Please input a number disivile by 7: ")
is_divisible = (x % 7 == 0)
if (is_divisible):
print("The number " + str(x) + " is divisble by 7.")
else:
print("The number " + str(x) + " not divisible 7. Please enter another number")


7)

Code
import re

num = raw_input("Please input a phone number in the form (XXX)-XXX-XXXX : ")
num = num.strip()

if(re.match(r'\(\d{3}\)\d{3}-\d{4}', num)):
print("Correct form! Area code: " + num[1:4])
else:
print("Please enter the number in proper form.")



8)

Code
name = raw_input("Please input your first and last name: ")
print("Your last name is: " + name.split()[1])



All tested and work on linux, don't see why they wouldn't work on any platform.
Member
Posts: 36,389
Joined: Jul 18 2008
Gold: 3,192.00
Jan 30 2017 06:40pm
Missed the first #5

Code
x = raw_input("Please input a number: ")
y = raw_input("Input another number: ")
print("first: " + x)
print("second: " + y)

tmp = x
x = y
y = tmp
print("Swapped!")
print("first: " + x)
print("second: " + y)
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jan 30 2017 10:09pm
There is no need for a temp variable for swapping in python, simply reassign the variables in reverse order. They will become (if I recall) named turples and reassign their data across them:

Code
>>> x = 1
>>> y = 2
>>> x, y = y, x
>>> x
2
>>> y
1


This post was edited by AbDuCt on Jan 30 2017 10:11pm
Member
Posts: 36,389
Joined: Jul 18 2008
Gold: 3,192.00
Jan 31 2017 05:47am
Oh cool, you posted the same thread in another subforum and didn't bother to say that the work was already done.

Gg
Member
Posts: 36,389
Joined: Jul 18 2008
Gold: 3,192.00
Feb 2 2017 12:07pm
Quote (Mastersam93 @ Jan 31 2017 06:47am)
Oh cool, you posted the same thread in another subforum and didn't bother to say that the work was already done.

Gg



Nvm, got paid. :cheers:
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll