0

I want to make this code much more elegant, using loop for getting user input into a list and also making the list as a list of floats withot having to define each argument on the list as a float by itself when coming to use them or print them... I am very new to python 3.x or python in general, this is the first code i have ever written so far so 'mercy me pardon!'

Name = "john"
Place = "Colorado"
print (("Hello %s What's up? \nare you coming to the party tonight in %s\n 
if not at least try to make simple calculator:") % (Name, Place))

print ("you will input 2 numbers now and i will devide them for you:")
calc =list(range(2))
calc[0] = (input("number 1:"))
calc[1] = (input("number 2:"))
print (float(calc[0])/float(calc[1]))

3 Answers3

1

Since you are saying you are new to Python, I'm going to suggest you experiment with a few methods yourself. This will be a nice learning experience. I'm not going to give you answers directly here, since that would defeat the purpose. I'm instead going to offer suggestions on where to get started.

Side note: it's great that you are using Python3. Python3.6 supports f-strings. This means you can replace the line with your print function as follows.

print(f"Hello {Name} What's up? "
"\nare you coming to the party tonight in {Place}"
"\n if not at least try to make simple calculator:")

Okay, you should look into the following in order:

  • for loops
  • List comprehension
  • Named Tuples
  • functions
  • ZeroDivisionError
kdheepak
  • 1,274
  • 11
  • 22
0

Are you looking for something like this :

values=[float(i) for i in input().split()]
print(float(values[0])/float(values[1]))

output:

1 2
0.5
0

By using a function that does the input for you inside a list comprehension that constructs your 2 numbers:

def inputFloat(text):
    inp = input(text)   # input as text
    try:                # exception hadling for convert text to float
        f = float(inp)  # if someone inputs "Hallo" instead of a number float("Hallo") will
                        # throw an exception - this try: ... except: handles the exception
                        # by wrinting a message and calling inputFloat(text) again until a
                        # valid input was inputted which is then returned to the list comp
        return f        # we got a float, return it
    except:
        print("not a number!") # doofus maximus user ;) let him try again
        return inputFloat(text) # recurse to get it again

The rest is from your code, changed is the list comp to handle the message and input-creation:

Name = "john"
Place = "Colorado"
print (("Hello %s What's up? \nare you coming to the party tonight in %s\n"+
        " if not at least try to make simple calculator:") % (Name, Place))

print ("you will input 2 numbers now and i will devide them for you:")

# this list comprehension constructs a float list with a single message for ech input
calc = [inputFloat("number " + str(x+1)+":") for x in range(2)] 
if (calc[1]):  # 0 and empty list/sets/dicts/etc are considered False by default
    print (float(calc[0])/float(calc[1]))
else:
    print ("Unable to divide through 0")

Output:

"
Hello john What's up?
are you coming to the party tonight in Colorado
 if not at least try to make simple calculator:
you will input 2 numbers now and i will devide them for you:
number 1:23456dsfg
not a number!
number 1:hrfgb
not a number!
number 1:3245
number 2:sfdhgsrth
not a number!
number 2:dfg
not a number!
number 2:5
649.0
"

Links:

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69