2

I'm a beginer in Python and I do not succeed to figure out how to solde the issues in my code. As my lecture do not provide any assistance, according to the online module that became mandatory because of the corona, I really need your help ! Before to go on the issues, there is the goals of my code :

The first function take as arguments 2 integers, and returns a random number between those integers. We will call this function get_rand_int. If the two integers are equals, this is an error and we will return None. (Hint: just put return None as your line for these cases.) We can test for the result of None in the second function and report that it was an error there. The second function, called print_random, asks for user input for one integer greater than 0, and if it receives good input, calls the first function. It converts the user input to an integer and calls the first function with 0 as the first argument and the user input as the second argument. We capture the output from the first function in a variable (e.g., rand = get_rand_int(0, input)). Our second function then prints out the number returned by the first function in a sentence: "Your random number is " and then the number.

For now, I think my code is in line with the goal ( just need to add something in the case of the first functiun would return "none") but I'm stuck with a small error which is making me stuck ! This error occurs when I'm trying to run the second function !

File "<ipython-input-83-f6b07c49d323>", line 2
    def print_random():
                       ^
SyntaxError: unexpected EOF while parsing

My code

import random

def get_rand_int(arg1, arg2):

    rand = random.randint(arg1, arg2)
    if float(rand) == float(arg1):
        return None
    elif float(rand) == float(arg1):
        return None
    else:
        print(rand)

def print_random():
    try :
        prompt = int(input("Please enter an integer greater than 0:"))
        assert(prompt > 0)

    except:
        print("Invalid input: You must input an integer greater than 0 for your input.")

    rand = get_rand_int(0, input)
    print( "Your random number is ",rand)

Thanks for your help !

DYZ
  • 55,249
  • 10
  • 64
  • 93
  • You seem to execute your code from Python command line. Make sure that the line after `print(rand)` is _empty_ and has no tabs or spaces. – DYZ Mar 29 '20 at 00:28
  • I copied and pasted your code in `vscode` and couldn't reproduce your error. I even invoke `print_random()` function. You have another issue there, you are passing `input` as an argument to `get_rand_int(0, input)`. I think you meant to pass `prompt`. Also, both `if` statement on the first function are the same. – DarK_FirefoX Mar 29 '20 at 00:29
  • 2
    Store your program in a file and run the file. It is very likely that _this_ problem will go away. – DYZ Mar 29 '20 at 00:30
  • And you are not returning anything. So `rand` on the first function will be `None` and you will print `None` – DarK_FirefoX Mar 29 '20 at 00:37

1 Answers1

0

You had two mistakes that were not terribly obvious. You tried passed the input function to your get_rand_int method and you forgot to return the rand value. Complete working code below:

import random

def get_rand_int(arg1, arg2):

    rand = random.randint(arg1, arg2)
    if float(rand) == float(arg1):
        return None
    elif float(rand) == float(arg1):
        return None
    else:
        print(rand)
        return rand # You need to return the random value

def print_random():
    try :
        prompt = int(input("Please enter an integer greater than 0:"))
        assert(prompt > 0)

    except:
        print("Invalid input: You must input an integer greater than 0 for your input.")

    rand = get_rand_int(0, prompt)  # You need to pass in the value return from input, not input func
    print( "Your random number is ", rand)
csteel
  • 428
  • 1
  • 3
  • 14