Jump to content

Trying to figure out how to grab user input, but it's not working

 

import functools


def gcd(a, b):
    """Return greatest common divisor using Euclid's Algorithm."""
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):
    """Return lowest common multiple."""
    return a * b // gcd(a, b)


def lcmm(*args):
    """Return lcm of args."""
    return functools.reduce(lcm, args)


def lcmm_input():
    x = input("Numbers to calculate LCM:\n")

    return x


print(lcmm_input())

 

so x would be something like 44, 55 then will go into the reduce()

 

but instead i get back 44, 55 and exit code 0

 

meanwhile if you put in 44, 55 directly into lcmm(), the algorithm will correctly return the LCM

Link to comment
https://linustechtips.com/topic/893294-python-userinput/
Share on other sites

Link to post
Share on other sites

16 minutes ago, Technicolors said:

so x would be something like 44, 55 then will go into the reduce()

 

but instead i get back 44, 55 and exit code 0

 

meanwhile if you put in 44, 55 directly into lcmm(), the algorithm will correctly return the LCM

Let's follow the control flow of this program.
 

print(lcmm_input())

This calls the built in function "print" and passes it the result of the command "lcmm_input()". So control of the program goes to the lcmm_input function:

 

def lcmm_input():
    x = input("Numbers to calculate LCM:\n")

    return x


This creates a variable "x", and assigns it the result of the function call "input()", which is passed the argument "Numbers to calculate LCM:\n". The user then enters any valid string, and the interpreter then stores that result in x.

Then we have "return x". This returns x (duh.)

 

Now we are back to the line:
 

print(lcmm_input())

lcmm_input() has returned the value x, so now x is passed to the built in function print, which prints the value stored in address x. 


Next, the program ends. 

So my question to you is: Why does it only print what you put into it, and do nothing else?

ENCRYPTION IS NOT A CRIME

Link to comment
https://linustechtips.com/topic/893294-python-userinput/#findComment-11011516
Share on other sites

Link to post
Share on other sites

@straight_stewie 

 

ah yea, forgot to call lcmm()

 

import functools
from math import gcd


def lcm(a, b):
    """Return lowest common multiple."""
    return a * b // gcd(a, b)


def lcmm(*args):
    """Return lcm of args."""
    return functools.reduce(lcm, args)


def lcmm_input():
    x = input("Numbers to calculate LCM:\n")

    return x


print(lcmm(lcmm_input()))
print(lcmm(4, 55, 77))

**removed gcd since there's already a math lib for it, functionally is the same

 

sadly, it doesn't do what i expect. 

 

if you see the first call, it'll print literally what the user just typed with no errors

the second call, it'll print 1540, which is the LCM according to the algorithm and the response i expect

 

i think when you say the user enters a valid string, x may only be a string so it's already reduced? meanwhile the second call contains multiple int args 

Link to comment
https://linustechtips.com/topic/893294-python-userinput/#findComment-11011533
Share on other sites

Link to post
Share on other sites

okay, i think i have the solution 

import functools
from math import gcd


def lcmm(args):
    """Return lcm of args."""
    return functools.reduce(lambda a, b: a * b // gcd(a, b), args)


def lcmm_input():
    x = [int(x) for x in input("Numbers: ").split()]

    return x


print(lcmm(lcmm_input()))

 

**further refactor by moving lcm() into lambda 

 

then removed the star operator from lcmm(args)

and lcmm_input() arranges x into a list

 

i enter 4, 55, 77 no commas, just spaces. and correctly prints 1540 

 

i think the issue was the star operator. which is odd because i read it means the amount of arguments are arbitrary. but i leave it in, i get

[4, 55, 77] 

 

Link to comment
https://linustechtips.com/topic/893294-python-userinput/#findComment-11011629
Share on other sites

Link to post
Share on other sites

36 minutes ago, Technicolors said:

i think when you say the user enters a valid string, x may only be a string so it's already reduced? meanwhile the second call contains multiple int args 

Python treats all console input as a string. There are no implicit conversions for strings. What you'll need to do is explicitly convert from the user input to integers/floats. Make sure to handle the case(s) where the user entered something like "super_stewie" instead of "1234". What should happen in your application when a user inputs "123Stewie"?

Sifting through user input is a painful but very necessary thing to learn. 
 

Method chaining is also an important skill to learn.
 

ENCRYPTION IS NOT A CRIME

Link to comment
https://linustechtips.com/topic/893294-python-userinput/#findComment-11011642
Share on other sites

Link to post
Share on other sites

2 minutes ago, straight_stewie said:

Python treats all console input as a string. There are no implicit conversions for strings. What you'll need to do is explicitly convert from the user input to integers/floats. Make sure to handle the case(s) where the user entered something like "super_stewie" instead of "1234". What should happen in your application when a user inputs "123Stewie"?

Sifting through user input is a painful but very necessary thing to learn. 
 

Method chaining is also an important skill to learn.
 

would i need to use "try... except" to handle those cases?

 

anyway i have a possible solution posted above, can you check? 

 

method chaining is new to me

Link to comment
https://linustechtips.com/topic/893294-python-userinput/#findComment-11011654
Share on other sites

Link to post
Share on other sites

1 minute ago, Technicolors said:

would i need to use "try... except" to handle those cases?

 

anyway i have a possible solution posted above, can you check? 

That solution should work, I'm reinstalling python now, I haven't needed it since I last reset my machine, and I've been focusing on .NET since that time.

Method chaining is simple. Infact, both of your posted solutions used it.

Simply, method chaining is the act of using a method call as an input argument to another method, such as:

print(lccm(args))

Which first calls lccm with args, and then calls print with the results of that method call. 

ENCRYPTION IS NOT A CRIME

Link to comment
https://linustechtips.com/topic/893294-python-userinput/#findComment-11011672
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

×