Wrong result | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Wrong result

Why is my code is giving me a wrong result? https://code.sololearn.com/cLsAhnEMoQgJ/?ref=app

27th Jun 2019, 2:34 PM
Suraj Chari
2 Answers
+ 3
The main issue you're having is that you're trying to change the values of certain variables from within a function. In your process() function, your reference to n uses your definition outside the function (i.e. line 6: n=0). It doesn't take the value of n defined in the main() function. You have two ways around this. Firstly you can create parameters for each function, meaning that you can pass variable from one function to another. For example: def process(x): while i < x: .... def main(): n = int(input()) process(n) This is a skeleton version of your code where n is taken as input in your main() function and passed as an argument to your process function. Secondly and much less advised, you can set variables as global as shown below. def process: while i < n: .... def main(): global n n = int(input()) process() .... Now n is global, so each function refers to the same variable.
27th Jun 2019, 3:25 PM
Russ
Russ - avatar
+ 5
It‘s a bit difficult to understand your code since the comments/docstrings are missing, Suraj Chari . I‘m guessing you‘re new to python as you are using a somewhat non-pythonic approach for this task. Instead, you can try the following: ———————————— # Input some numbers seperated by space in the first line (e.g. 5 7 12 15 20) in = input.split() int(max(in)) ———————————— This will print the largest value in the list. Hope this helps 😊
27th Jun 2019, 2:50 PM
aceisace
aceisace - avatar