Jump to content

python finding largest number

xGear

Evening again everyone! Pythons really trolling me this week haha....anyways the following won't work, it supposed to find the largest number out of 5 inputed numbers. It works for finding the smallest and I just reversed the signs to > and it just wont work, any suggestions appreciated. Cheers.

 

A = int(input ("Enter first number"))
B = int(input ("Enter second number"))
C = int(input ("Enter third number"))
D = int(input ("Enter fourth number"))
E = int(input ("Enter fifth number"))
 
 
if A > B:
    if A > C:
        if A > D:
            if A > E:
                print (A)
elif B > A:
    if B > C:
        if B > D:
            if B > E:
                print (B)
elif C > A:
    if C > B:
        if C > D:
            if C > E:
                print ©
elif D > A:
    if D > B:
        if D > C:
            if D > E:
                 print (D)
else:
    if E > A:
        if E > B:
            if E > C:
                if E > D:
                    print (E)
 
Link to comment
Share on other sites

Link to post
Share on other sites

 

Evening again everyone! Pythons really trolling me this week haha....anyways the following won't work, it supposed to find the largest number out of 5 inputed numbers. It works for finding the smallest and I just reversed the signs to > and it just wont work, any suggestions appreciated. Cheers.

 

A = int(input ("Enter first number"))
B = int(input ("Enter second number"))
C = int(input ("Enter third number"))
D = int(input ("Enter fourth number"))
E = int(input ("Enter fifth number"))
 
 
if A > B:
    if A > C:
        if A > D:
            if A > E:
                print (A)
elif B > A:
    if B > C:
        if B > D:
            if B > E:
                print ( B)
elif C > A:
    if C > B:
        if C > D:
            if C > E:
                print ©
elif D > A:
    if D > B:
        if D > C:
            if D > E:
                 print (D)
else:
    if E > A:
        if E > B:
            if E > C:
                if E > D:
                    print (E)

 

mmm so it put emojes in....the prints are meant to be the letter in alphabetical order, sorry about that!

Link to comment
Share on other sites

Link to post
Share on other sites

int value = 0for x in list    if x>value        value=x

this is just a hint, not the exact answer

work wit this and you should understand a bit better how to do that :)

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
Share on other sites

Link to post
Share on other sites

That's not a very good way of doing it...

 

let's say your numbers are in a list called list.

a = 0for(i in range(list.length()))   if list[i] > a      a = list[i]print(a)

this will have the same effect in a fifth of the lines (I'm not sure I remember the syntax too well but it should be clear)

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

That's not a very good way of doing it...

 

let's say your numbers are in a list called list.

a = 0for(i in range(list.length()))   if list[i] > a      a = list[i]print(a);

this will have the same effect in a fifth of the lines (I'm not sure I remember the syntax toow ell but it shiould be clear)

I am very basic lol.....what I have should work although long winded, it worked for the negative numbers....

Link to comment
Share on other sites

Link to post
Share on other sites

I am very basic lol.....what I have should work although long winded, it worked for the negative numbers....

 

the last if cycle is unnecessary regardless, if it got to that point then E is obviously the highest number. By the same logic, you can cut every if after the first one:

if A > B:    if A > C:        if A > D:            if A > E:                print (A)elif B > C:     if B > D:         if B > E:              print (B)elif C > D:     if C > E:         print (C)elif D > E:    print (D)else:   print (E)

What error does your original code give?

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

the last if cycle is unnecessary regardless, if it got to that point then E is obviously the highest number. By the same logic, you can cut every if after the first one:

if A > B:    if A > C:        if A > D:            if A > E:                print (A)elif B > C:     if B > D:         if B > E:              print (B)elif C > D:     if C > E:         print (C)elif D > E:    print (D)else:   print (E)

What error does your original code give?

Thanks....it doesn't give an error it just doesn't output anything.

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks....it doesn't give an error it just doesn't output anything.

Ah, stupid mistake.

Example of what you need to do:

A = int(input ("Enter first number")) --what you didA = int(raw_input("Enter first number")) --what I believe you need to do

If I'm not terribly wrong

Sig under construction.

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks....it doesn't give an error it just doesn't output anything.

 

you probably messed up one of the conditions, check it again.

 

but you should really go for the other solution, and learn to use cycles instead of multiple ifs whenever possible. now you have 5 numbers, what if you had 1000?

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

Ah, stupid mistake.

Example of what you need to do:

A = int(input ("Enter first number")) --what you didA = int(raw_input("Enter first number")) --what I believe you need to do

If I'm not terribly wrong

 

input() is for Python 3

raw_input() is for Python 2

Link to comment
Share on other sites

Link to post
Share on other sites

Ah, stupid mistake.

Example of what you need to do:

raw_input was renamed to just input in Python3.

 

 

edit: or you could just do

print(max([int(input('Enter {} number: '.format(['first','second','third','fourth','fifth'][x]))) for x in range(5)]))

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

input() is for Python 3

raw_input() is for Python 2

 

 

raw_input was renamed to just input in Python3.

 

The original code didn't work because you would need to use >= instead of just >.

Ah. Our computing teacher seems to swear by Python 2 so I have no clue about Python 3.

Sig under construction.

Link to comment
Share on other sites

Link to post
Share on other sites

 

raw_input was renamed to just input in Python3.

 

 

edit: or you just just do

print(max([int(input('Enter {} number: '.format(['first','second','third','fourth','fifth'][x]))) for x in range(5)]))

>= doesn't work either thanks

Link to comment
Share on other sites

Link to post
Share on other sites

>= doesn't work either thanks

Ya sorry, misread and thought you had all ifs. 

 

It doesn't work because if A>B but A<C for example your code drops out of the if statements without doing anything. I'd suggest just using the max() function.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Ya sorry, misread and thought you had all ifs. 

 

It doesn't work because if A>B but A<C for example your code drops out of the if statements without doing anything. I'd suggest just using the max() function.

Ok thanks

Link to comment
Share on other sites

Link to post
Share on other sites

What you should be doing is saving the inputs in a list. Then you can sort the list and print the last element, which will be the largest.

I own and use, sorted from newest to oldest: SteelSeries 6Gv2. Microsoft SideWinder X4. Mionix Naos 7000. Zowie EC1 Evo. Microsoft SideWinder X8. Microsoft IntelliMouse Explorer 3.0. Dell U2414H. Samsung P2270H. AKG K273 Pro. Sennheiser HD555. Razer Goliathus Speed Medium. Func 1030 L. Qpad CT Medium.

I used to own: Razer DeathAdder 3G. Razer Krait. IntelliMouse Optical 1.1. SteelSeries QcK.

Link to comment
Share on other sites

Link to post
Share on other sites

I would also recommend learning about boolean operations.

# Your code becomes this (with the logical errors fixed)if A >= B and A >= C and A >= D and A >= E:    print (A)elif B >= A and B >= C and B >= D and B >= E:    print (B)elif C >= A and C >= B and C >= D and C >= E:    print (C)elif D >= A and D >= B and D >= C and D >= E:    print (D)elif E >= A and E >= B and E >= C and E >= D:    print (E)

There are still better ways of getting the max value, however this will at least help you learn a bit more about working with if statements and booleans.

Link to comment
Share on other sites

Link to post
Share on other sites

Will still fail if 2 of the values are both the max.

 

Whoops...I'll have to edit it.

Link to comment
Share on other sites

Link to post
Share on other sites

Come on guys all ifs? 

 

best way is to store the numbers in an array and use loops to bubble sort.

 

here is it in c# i don't know python

int[] numbers = new int[] {1, 5, 3, 4, 2};foreach(int nums in numbers){    for(int i=0;i < numbers.Length-1;i++)    {        int temp;                if(numbers[i] > numbers[i+1])        {            temp = numbers[i+1];            numbers[i+1] = numbers[i];            numbers[i] = temp;        }    }}foreach(int nu in numbers){ Console.WriteLine(nu);}

Output

 

1,2,3,4,5

 

or as you need just print out the highest number as it will always been at the end of the array 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Come on guys all ifs?

 

If the goal is to understand how if statements and boolean expressions work, then sure, it's an acceptable exercise.

 

Alternatives were also mentioned, including sorting, which isn't the "best way" in many cases.

Link to comment
Share on other sites

Link to post
Share on other sites

If the goal is to understand how if statements and boolean expressions work, then sure, it's an acceptable exercise.

 

Alternatives were also mentioned, including sorting, which isn't the "best way" in many cases.

I guess, sorting seems better then having lots of ifs, simple options tend to be better.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
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

×