help needed python 3

This is the place for queries that don't fit in any of the other categories.

help needed python 3

Postby Donovanx » Fri May 10, 2013 3:06 pm

Hi all I'm new to the furom and python.
I'm using python 3.I'm curuntly just playing around with some basics and I'm trying to make a simple calculator wich lator on I can ad graphix and mouse functions as I get to the.
Here's what I got so far this is not home work (read something about stating if it is or not in an admin post)
Code: Select all
#calculator
print("This is a simple calculator made by Donovan Foxcroft!")
dig1 = input("type your fist amount here")
dig2 = input("type your second amount here")
op1 = int(input(" "))
1 == "+"
2 == "-"
3 == "/"
4 == "*"
if op1 == 1:
    print(dig1 + dig2)
elif op1 == 2:
        print(dig1 - dig2)
elif op1 == 3:
        print(dig1 / dig2)
elif op1 == 3:
        print (dig1 * dig2)

Ok so I've got a few idess but don't know how to do it.
First if you run the code you wil get some errors like typeerror: unsupported operand type(s) for -:'str' and 'str'
Wat I want is if you guys know how give me poseble salutions.
And do you guys know how to make the input limited to operators , number or certain leters?
And one last think I know its of topic but I've done most of the basics like list lupel if statements elif else print and a few more do you guys know a good free ebook so that I can learn more?
Srry for the spelling.
And thank you for taking the time to read this and any help!
Donovanx
 
Posts: 13
Joined: Fri May 10, 2013 1:54 pm

Re: help needed python 3

Postby metulburr » Fri May 10, 2013 3:22 pm

i am not sure what the intention is of this:
Code: Select all
1 == "+"


I modified your code a bit.
Code: Select all
print("This is a simple calculator made by Donovan Foxcroft!")
dig1 = float(input("type your fist amount here"))
dig2 = float(input("type your second amount here"))
op1 = input("Operation: ")

if op1 == '+':
    ans = dig1 + dig2
elif op1 == '-':
    ans = dig1 - dig2
elif op1 == '/':
    ans = dig1 / dig2
elif op1 == '*':
    ans = dig1 * dig2
   
print('{:.1f}'.format(ans))


so first i converted dig1 and dig2 to floats before they are assigned. op1 i left input() as a string becqause the if condition is checking for a string to choose the operation. Then i assigned each an answer so there are not numerous print statements, but just one to modify the output look. Then finally i printed the output using hte format method.
which to veiw the original output would just be
Code: Select all
print('{}'.format(ans))

The
Code: Select all
:.1f
formats the output to one decimal place on the print

and then an example output:
Code: Select all
This is a simple calculator made by Donovan Foxcroft!
type your fist amount here1.1
type your second amount here2.4
Operation: +
3.5


EDIT:
And do you guys know how to make the input limited to operators , number or certain leters?

here are some small examples. The first two do the same just differnet ways. The third uses the in operator to check if the input is "in" the list to decided what to do with it at that point.
Code: Select all
while True:
    choice = input('Enter number 1')
    try:
        float(choice)
        break
    except ValueError:
        continue


ch = input('Enter number 2')
if not ch.isdigit():
    print('{} is not a digit'.format(ch))
else:
    print('thank you for listening')


allow = ['a','b','c']
ch = input('Enter a b or c')
if ch in allow:
    print('ok')
else:
    print('you did not enter correct data')
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1512
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: help needed python 3

Postby Donovanx » Fri May 10, 2013 3:42 pm

Ty perfect one or two thing I ddnt know since the ebook I used basicly just expained some indents and basics basics but I wil learn and find out in the python docs the stuff I don't know one last question is there anyway I can make it that I can type the whole (sum)in one line ? If I can I'm geusing I'll have to have some seperator for the computer to tel the dif between dig1,dig2,op1 and then do the sum? Hope you understand lol english is not my first lang
Donovanx
 
Posts: 13
Joined: Fri May 10, 2013 1:54 pm

Re: help needed python 3

Postby metulburr » Fri May 10, 2013 3:58 pm

If I can I'm geusing I'll have to have some seperator for the computer to tel the dif between dig1,dig2,op1 and then do the sum?


You mean like print the whole equation?
Code: Select all
print('{} {} {} = {:.1f}'.format(dig1, op1, dig2, ans))
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1512
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: help needed python 3

Postby Donovanx » Fri May 10, 2013 4:14 pm

Precisly wat I wanted ty very much
Donovanx
 
Posts: 13
Joined: Fri May 10, 2013 1:54 pm


Return to General Coding Help

Who is online

Users browsing this forum: W3C [Linkcheck] and 2 guests