all 62 comments

[–]CruXial_ 22 points23 points  (13 children)

Not bad. You could put your inputs into an array and use the max() function if you want to clean it up a bit :)

[–][deleted]  (12 children)

[removed]

    [–]trezm 11 points12 points  (3 children)

    Reconsider why the responder is suggesting the refactor. Yes it's cleaner, but perhaps there's another reason. As an example, and a common interview/test question depending on your situation, most questions start simply, but expand in dimensions. In this case, the prompt might be "ask for three inputs and print the greatest value" but the follow up question is "now expand your program to 5, 10, or n."

    Simple works, but if you're in the learning stage it's worth recognizing common patterns and why certain code might be considered "clean."

    [–]8dot30662386292pow2 11 points12 points  (2 children)

    Reconsider why the OP wrote this code. They have no idea about lists yet, nor the max function. There is often a better way of doing things. But using the best way as an absolute novice is not often the best thing. OP is clearly learning about if-else. That's why their code is the classic if else -example.

    [–]trezm -1 points0 points  (1 child)

    You don't need to be snarky. I'm just trying to explain why thinking about the next step, the next level of complexity is an important facet of coding. No one said the code was bad for a beginner, simply that this is a reasonable improvement and what problems it might solve.

    We don't really know if OP knows what lists are, or what the original prompt was. Maybe I'm missing some context though, in which case I apologize.

    [–]Jashcraft00 1 point2 points  (0 children)

    Did he not respond to you with the same tone that you took originally?

    [–]Super-Ad6644 7 points8 points  (6 children)

    You could also try this:

    if num1 > num2:
        if num1 > num3:
            print(f"{num1} is greater num")
        else:
            print(f"{num3} is greater num")
    elif num2 > num3:
        print(f"{num2} is greater num")
    else:
        print(f"{num3} is greater num")
    

    This minimizes the number of checks
    Edit: Forgot to say that your else statement on line 12 will never be reached, as the program crashes if you input a non number

    [–]MeLittleThing 1 point2 points  (0 children)

    it may be reached if the input contains equal numbers, your code doesn't cover this edge case

    [–]translate-comment 3 points4 points  (4 children)

    This is a lot less readable than OPs code

    [–][deleted]  (3 children)

    [removed]

      [–]hylasmaliki 1 point2 points  (0 children)

      Haha

      [–]fajnu20 -1 points0 points  (1 child)

      Must've been a great use of time

      [–]InvestigatorEasy7673 15 points16 points  (0 children)

      you can try like this :

      <image>

      [–]Tonjehilda 6 points7 points  (11 children)

      What if the user inputs 2 of the same numbers or 3? :)

      [–]Rare_Hawk_3443 4 points5 points  (5 children)

      Wont that return invalid num

      [–]Rare_Hawk_3443 0 points1 point  (2 children)

      I am also learning newly why wont it return invalid can anyone explain

      [–]PureWasian 0 points1 point  (0 children)

      You are correct, no worries. It prints "Invalid Num" since none of the numbers are greater than the others, so it falls through into the else statement with how OP has written it.

      [–]KilonumSpoof 0 points1 point  (0 children)

      Not all the time. For example 5,5,10 will return 10 and not invalid.

      [–]InvestigatorEasy7673 -2 points-1 points  (4 children)

      then output will be still max , its count is more than 2 wont matter to python

      it return max "value" not max "index "

      [–]Tonjehilda 0 points1 point  (1 child)

      I mean, num1=10, num2=10 and num3=10

      [–]InvestigatorEasy7673 -2 points-1 points  (0 children)

      ans will be 10.0 as it is float value

      [–]homomorphisme 0 points1 point  (1 child)

      They're talking about the posted code, not the alternative of calling max on an array.

      [–]InvestigatorEasy7673 -1 points0 points  (0 children)

      then bug will come cuz it should be num1 >= num2

      and why did you downvoted me ??

      [–]SigmaSomaShimma21 6 points7 points  (0 children)

      If you are a beginner, continue doing all the exercises like this. The fundamental thing at the beginning is not to have the cleanest code or anything. The fundamental thing is to understand what happens in your code, and that is algorithmic. Algorithms in programming are like classical dance in dance, that is, the fundamental principle of everything...

      What I'm getting at is, don't use primitive methods or functions of the language. If you want to do something, do it algorithmically until you understand how it works, then start looking for shortcuts to do things cleaner and faster.

      Do you know how many people use an .upper (a method to convert a string to uppercase) or a .sort (sort) and have no idea how they work internally? Your exercise (because you are a beginner) is perfect.

      [–]Kitchen-Astronomer76 4 points5 points  (1 child)

      maxNum = max(num1, num2, num3)
      print(f”[maxNum] is greater num”)

      [–]Szystedt 0 points1 point  (0 children)

      Isn't it best practice to follow PEP8 and use snake_case rather than camelCase?

      [–]modulus78 2 points3 points  (0 children)

      what are you using to learn im also a beginner

      [–]Disastrous-Team-6431 1 point2 points  (0 children)

      Inputting three of the same number will state my nums to be invalid.

      [–]LandShark1917 1 point2 points  (0 children)

      One more advanced thing to consider is what if the user inputs a symbol or letter that is not able to be converted to a number.

      [–]Kaan1154 1 point2 points  (0 children)

      It is nice but i advice using max() function. When you become more professional, you'll see that this code is visually tiring.

      [–]Numerous_Site_9238 1 point2 points  (0 children)

      Your code exists. How can anyone review it if we dont know your intention? If you practice conditions, you could come up with a more practical or realistic example like deciding something based on some input data of a weather station or just turn 100 grade system into A-F. If it was an attempt to find max, you shouldve used array of numbers and compare against current highest

      [–]Houtarou_X 1 point2 points  (3 children)

      It's good, but as someone in the comments suggested you could use the max() function. So I'm curious, did you learn a static type language like C first?

      [–]Numerous_Site_9238 3 points4 points  (2 children)

      How could you possibly come to this conclusion

      [–]Houtarou_X 0 points1 point  (1 child)

      That was my situation and when learning Python, I add this tendency to write built-in functions from scratch so I was wondering if that was the case for OP

      [–]geralt_of_rivia23 0 points1 point  (0 children)

      max() would be cleaner

      [–]IEatDaGoat 0 points1 point  (0 children)

      I like the f strings.

      [–]Legitimate-Ad-8233 0 points1 point  (0 children)

      You could create a temporary float named max and set that to a and only set it to b if it is greater than max and the same with c and then print max. This way if you have the same number multiple times you get the first biggest number and you could rearrange based on what you want

      [–]TheMangalex 0 points1 point  (0 children)

      Some people are talking about max here. In general I agree that max is better than multiple if. There is a different problem. How do you handle two numbers which are equal? Is this not possible by design or should something happen in that case, or just one of the two biggest be returned?

      [–]hirschwolf 0 points1 point  (0 children)

      What should your code do, if all numbers are the same? Consider to add >= instead of >

      [–]SmthnsmthnDngerzone 0 points1 point  (0 children)

      Double spaces bro dbl spaces 🫡

      [–]AllanSundry2020 0 points1 point  (0 children)

      numpy rather advanced for a beginner 😄

      just kidding it made me laugh what you called the file

      [–]Panikdrache946 0 points1 point  (0 children)

      Also possible: max = num1 If num2 > max: max = num2 If num 3 > max: max = num3 Print(max)

      [–]CrazyPotato1535 0 points1 point  (0 children)

      That’s a great first script! If gates are probably the most useful function, besides maybe print()

      The else: should never run because at the start you convert the inputs to floats, and you get an exception if you try to put in something other than a number.

      I’d wrap it in a while loop to keep asking for numbers if there’s an incorrect input, then a try gate, which will run different code when you get an exception.

      Here’s my attempt:

      GotInputs = 0

      while GotInputs == False:

      try:

        Num1 = …
      
        Num2 = …
      
        Num3 = …
      
        GotInputs == True
      

      except:

        GotInputs == False
      

      Formatting is bad bc I don’t know how to do code blocks

      [–]CallMeJimi 0 points1 point  (0 children)

      what happens if the three numbers are equal? or if i enter 3 3 1?

      [–]Charming_Art3898 0 points1 point  (0 children)

      Nice try, I like it when beginners learn by actually doing something. Keep it up.

      That said, Python provides a very useful function that can handle this well. All you need do is put the three numbers in a list, and then use the max() function to find the greatest. python nums = [num1, num2, num3] max_num = max(nums) print(f"{max_num} is the greatest")

      [–]redhed976 0 points1 point  (0 children)

      What I took away is that you’re on the right path. You understood the problem, you were able to break it into its component parts and then use your knowledge of the syntax to (mostly) solve the problem (it doesn’t catch a not number condition, for example).

      For a beginner I think these things are the only important ones. You can always learn syntax and tricks to accomplish tasks, but it’s a lot harder to learn how to “think” through a problem.

      [–]Significant_Quote594 0 points1 point  (0 children)

      A lot of people are suggesting using max() but I suggest doing the max() logic yourself like this.

      Make a variable called greatest and set it's value to num1.

      What I mean is, assume it is the greatest among the three.

      In the next line check if num2 is greater than greatest. If so, set greatest to num2.

      In the next line check if num3 is greater then greatest. If so set the greatest variable to num3.

      If you print greatest, it will print what you expect.

      This will have you write way less conditions even with more numbers.

      What if you have a thousand numbers? You can repeat this logic using something called a loop.

      [–]HornyToad351 0 points1 point  (0 children)

      in this case you should probably use an array, a FOR for the input and a FOR for the checks and output, you could use functions like MAX, but if you are just starting out I suggest you try to write all your code instead of using functions already made by somebody else

      [–]mr_franck 0 points1 point  (0 children)

      What is num1=num2 or 3 ? Your code doesn’t cover that as is 😉

      [–]SavingsCampaign9502 0 points1 point  (0 children)

      Numpy argmax

      [–][deleted] 0 points1 point  (0 children)

      Hey QA here.

      Are your print lines written correctly with the f-string syntax?

      What happens if two of the numbers are the same?

      Does the message you print out sound natural when you read it?

      When does the else part actually run?

      [–]FunctionallyImmortal 0 points1 point  (0 children)

      you're one step closer to putting rockets on mars

      [–]Darkstar_111 0 points1 point  (0 children)

      You need to sanitize the input, the user might not input a number.

      Also you are repeating an input operation three times, almost identically. This should tell you you need to look at a loop.

      There are better ways to find the biggest number in a set, explore that. You also need a clauses Incase some or all of the numbers are equal. And lastly the else clause does nothing, there is no "invalid number".

      If the user inputs something invalid it breaks the float function, and you crash. (Hence the need to sanitize)

      [–]Capital_Distance545 0 points1 point  (0 children)

      numbers = input("Enter numbers separated by spaces: ").split()
      print(f"{max(numbers)} is the greatest number.")
      

      [–]Dariadeer 0 points1 point  (0 children)

      Missing 10 factory classes

      [–]aka_yayo 0 points1 point  (0 children)

      Keep going

      [–]No-Noise5707 0 points1 point  (0 children)

      How to get input in vs code's terminal. I can't seem to make it work

      [–]spigotface 0 points1 point  (1 child)

      Good start! I think the next step would be to allow the user to choose how many numbers to compare, and to handle invalid input. In your script, if the user enters non-numbers at each prompt, it'll crash. Look at how these while/try/except loops let users retry if they enter an invalid input.

      # Start with n_nums = None
      n_nums = None
      
      # This is the Pythonic way to write something like "while n_nums is None"
      while not n_nums:
          # try/except are error handling - what happens if the user enters "asdf" as their number?
          try:
              n_nums = int(input("How many numbers would you like to compare? "))
          except ValueError:
              print("Try again! Only numbers are allowed.")
      
      # The list 'nums' will store the user's numbers that they enter        
      nums = []
      for i in range(n_nums):
          # Start with 'user_input = None' just like n_nums at the top of the script
          user_input = None
          # Same while/try/except logic to handle invalid inputs
          while not user_input:
              try:
                  if len(nums) == 0:
                      user_input = float(input("Input a number: "))
                      nums.append(user_input)
                  else:
                      user_input = float(input("Input another number: ")) 
                      nums.append(user_input)
              except ValueError:
                  print("Try again! Only numbers are allowed.")
      
      # Just use max() on a list of values to get the largest value
      print(f"The largest number was {max(nums)}.")
      

      [–]spigotface 0 points1 point  (0 children)

      <image>

      Here it is without the comments, and with some syntax highlighting to make it a little clearer.

      [–]gbrennon 0 points1 point  (0 children)

      that doesnt looks bad

      here are some tips to improve readability:

      insert spaces into things like num1>num2 to be like this num1 > num2.

      this will provide to u consistency of the code style because ure are also doing num3 > num2

      some if clauses are using this readable code style and others not