Jump to content

Help me with Python

Electric_Boi

Hello, LTT community.

I hope you can help me resolve a little puzzle, so I started getting in Python (a month ago). I was pretty confident that I can code a little piece of software. I started with calculator.

This is my current code :

Quote

print("======================================")
print("     Kalkulator (Development State)")
print("======================================")
#Matematičke operacije
def zbrajanje(x, y):
    return x + y
def oduzimanje(x, y):
    return x - y
def mnozenje(x, y):
    return x * y
def dijeljenje(x, y):
    return x / y
#Navigacija
print("Lista operacija")
print("1. Zbrajanje")
print("2. Oduzimanje")
print("3. Mnozenje")
print("4. Dijeljenje")
#Input korisnika
choice = input("Odaberi operaciju (1/2/3/4):")
a = int(input("Unesi prvi broj: "))
b = int(input("Unesi drugi broj: "))
if choice == '1':
    print(a, "+", b,"=", zbrajanje(a,b))
elif choice == '2':
    print(a, "-", b,"=", oduzimanje(a,b))
elif choice == '3':
    print(a, "*", b,"=", mnozenje(a,b))
elif choice == '4':
    print(a, "/", b,"=", dijeljenje(a,b))
else:
    print("Invalid input")

I created basic functions to add, subtract, multiply and divide (More advanced math will be implemented later in software).

Every time software finishes his tasks it quits, I want it to go back to start menu.

Is there any way to do it without rewriting whole code ?

BTW : Software is on Serbian

Edited by Electric_Boi
Note that software is in different language
Link to comment
Share on other sites

Link to post
Share on other sites

while(True):
	#Navigacija
	print("Lista operacija")
	print("1. Zbrajanje")
	print("2. Oduzimanje")
	print("3. Mnozenje")
	print("4. Dijeljenje")
	print("Q. Exit") #Whatever exit is in your language
	#Input korisnika
	choice = input("Odaberi operaciju (1/2/3/4/Q):")
	if choice == 'q':
		break
	a = int(input("Unesi prvi broj: "))
	b = int(input("Unesi drugi broj: "))
	if choice == '1':
		print(a, "+", b,"=", zbrajanje(a,b))
	elif choice == '2':
		print(a, "-", b,"=", oduzimanje(a,b))
	elif choice == '3':
		print(a, "*", b,"=", mnozenje(a,b))
	elif choice == '4':
		print(a, "/", b,"=", dijeljenje(a,b))
	else:
		print("Invalid input")

 

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, WereCatf said:

-snip-

Thank you so much, it works just as I expected.

Link to comment
Share on other sites

Link to post
Share on other sites

Quote


	print("Q. Exit") #Whatever exit is in your language

For this I woyld just have an elif saying break or something

important figure in mensa

Link to comment
Share on other sites

Link to post
Share on other sites

@WereCatf Hey, thanks for help, do you know any way to connect 2 python files in code line ?

I want to seperate some parts of that software (Geometry, algebra...).

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Electric_Boi said:

@WereCatf Hey, thanks for help, do you know any way to connect 2 python files in code line ?

I want to seperate some parts of that software (Geometry, algebra...).

 

If you mean use code of one python file in another, import them as if it was a module. Assuming you have A.py and B.py in the same folder you can do

A.py

def some_function():
	print('Bla')

B.py

import A

A.some_function()

or another way

from A import some_function

some_function()

 

Crystal: CPU: i7 7700K | Motherboard: Asus ROG Strix Z270F | RAM: GSkill 16 GB@3200MHz | GPU: Nvidia GTX 1080 Ti FE | Case: Corsair Crystal 570X (black) | PSU: EVGA Supernova G2 1000W | Monitor: Asus VG248QE 24"

Laptop: Dell XPS 13 9370 | CPU: i5 10510U | RAM: 16 GB

Server: CPU: i5 4690k | RAM: 16 GB | Case: Corsair Graphite 760T White | Storage: 19 TB

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×