Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,747 members, 7,817,061 topics. Date: Saturday, 04 May 2024 at 02:19 AM

Can Someone Help Me With A Solution To This Python Problem - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Can Someone Help Me With A Solution To This Python Problem (1059 Views)

Who Can Help Out With This Python Problem? / I need assistance with this Python Problem / Python Masters, Kindly Help Me Rectify This Python Problem. (2) (3) (4)

(1) (Reply) (Go Down)

Can Someone Help Me With A Solution To This Python Problem by jjmk(m): 2:47pm On Apr 27, 2020
I need to know more about input(). I don't seem to understand it clearly.

There are two numbers one higher than the other. Let's say 40 and 20 respectively.

Num1 = int(input("Enter the first number: "wink)
Num2 = int(input("Enter the second number: "wink)

# take the larger number

If num1 > num2:
Larger_num = num1
Else:
Larger_num = num2

# Print the result
Print("The larger number is:" Larger_num)

Pls guys help...I am stuck here.
Re: Can Someone Help Me With A Solution To This Python Problem by Jayess: 2:58pm On Apr 27, 2020
Num1 = int(input("Enter the first number: "wink
Num2 = int(input("Enter the second number: "wink

# take the larger number

If Num1 > Num2:
print ('say something')
Else:
print ('do something else')

# Print the result
print("The larger number is: " + Num1 )


Python is case sensitive and don't forget your 4 spaces index.....

Too many bugs in this your mini code!! cry I couldn't count undecided
Re: Can Someone Help Me With A Solution To This Python Problem by silento(m): 5:17pm On Apr 27, 2020
num1 = int(input("Enter number one "wink)
num2 = int(input("Enter number two "wink)

largenum = 0

If(num1 >= num2):
largenum = num1

else:
largenum = num2

Print(largenum," is the large number"wink

1 Like

Re: Can Someone Help Me With A Solution To This Python Problem by jjmk(m): 5:31pm On Apr 27, 2020
[quote author=Jayess post=88913484]Num1 = int(input("Enter the first number: "wink
Num2 = int(input("Enter the second number: "wink

# take the larger number

If Num1 > Num2:
print ('say something')
Else:
print ('do something else')

# Print the result
print("The larger number is: " + Num1 )


Python is case sensitive and don't forget your 4 spaces index.....

Too many bugs in this your mini code!! cry I couldn't count undecided[/quote

Sorry, its the way I posted it.
Below is the same script in my pastebin account..�

https://pastebin.com/YqepaJPU

U can help check it there.
Re: Can Someone Help Me With A Solution To This Python Problem by spartan117(m): 6:14pm On Apr 27, 2020
The code is quite simple.

You use pythons input method to request user input which will come as a string, hence the int keyword to convert from string to integer.

You then use if else statements to compare both numbers and save the greater number to a variable.

Then you print a concatenated string to show the result.

What's there not to understand?
Re: Can Someone Help Me With A Solution To This Python Problem by Sammarshall(f): 7:13pm On Apr 27, 2020
Hi, your coding is correct based on what you wish to achieve but you’re having syntax error because your input is stored on Num1 and Num2 respectively,

while the variable you’re checking your conditions with are written like this num1 and num2.

Those are different variables because Python just like most programming languages is case sensitive.

Please don’t rush your learning process, take it one step at a time to avoid loosing interest.

I wish you best of luck, programming is hard but it can be fun if you can be patient.

1 Like

Re: Can Someone Help Me With A Solution To This Python Problem by scarplanet(m): 10:28pm On Apr 27, 2020
jjmk:
I need to know more about input(). I don't seem to understand it clearly.

There are two numbers one higher than the other. Let's say 40 and 20 respectively.

Num1 = int(input("Enter the first number: "wink)
Num2 = int(input("Enter the second number: "wink)

# take the larger number

If num1 > num2:
Larger_num = num1
Else:
Larger_num = num2

# Print the result
Print("The larger number is:" Larger_num)

Pls guys help...I am stuck here.


First, your code is 95% correct. However Like you have been already told, variable names are case sensitive. You started with Num1 and Num2; then i switched to num1 and num2. Red flag!

Secondly, python builtin functions always start in lower case... have that imprinted at the back of your mind. Therefore "if" and "else" should start in lower case, "print" also.

Third, I have seen a lot of young programmers use int() with the input() function. That is TOTALLY WRONG. Let me tell you why. If in this your code you are to find the larger number between 20.2 and 20.3. When you use int() with input(), then both values will be rounded to the nearest integer and 20 will be returned. You see, the goal is defeated. I have also seen some people say you can use float() with input() if you want to enter a floating number. However, remember that you don't write a code for your own use, it is for others to use and maybe reproduce. So in a case where u have used float() and a user of your code enters 20 in the input, it is automatically converted into 20.0 float value. This alone is enough to distort the final result. Note that in programming, 20 and 20.0 are not thesame!
So then, what should you use? Well not many person might know, but whenever you want to return a non-string value with the input() function, the best function to use is eval(). To be used as eval(input("say something" )).

Cheers

3 Likes

Re: Can Someone Help Me With A Solution To This Python Problem by farmsata(m): 11:49pm On Apr 27, 2020
num1 = eval(input("Enter number one "wink)
num2 = eval(input("Enter number two "wink)
largenum = 0
if num1 >= num2:
largenum = num1
else:
largenum = num2
print(str(largenum) + " is the large number"wink

1 Like

Re: Can Someone Help Me With A Solution To This Python Problem by scarplanet(m): 12:00am On Apr 28, 2020
farmsata:
num1 = eval(input("Enter number one "wink)
num2 = eval(input("Enter number two "wink)
largenum = 0
if num1 >= num2:
largenum = num1
else:
largenum = num2
print(str(largenum) + " is the large number"wink


if num1 == num2, then num1 cannot be the larger number??
Re: Can Someone Help Me With A Solution To This Python Problem by silento(m): 12:41am On Apr 28, 2020
scarplanet:


First, your code is 95% correct. However Like you have been already told, variable names are case sensitive. You started with Num1 and Num2; then i switched to num1 and num2. Red flag!

Secondly, python builtin functions always start in lower case... have that imprinted at the back of your mind. Therefore "if" and "else" should start in lower case, "print" also.

Third, I have seen a lot of young programmers use int() with the input() function. That is TOTALLY WRONG. Let me tell you why. If in this your code you are to find the larger number between 20.2 and 20.3. When you use int() with input(), then both values will be rounded to the nearest integer and 20 will be returned. You see, the goal is defeated. I have also seen some people say you can use float() with input() if you want to enter a floating number. However, remember that you don't write a code for your own use, it is for others to use and maybe reproduce. So in a case where u have used float() and a user of your code enters 20 in the input, it is automatically converted into 20.0 float value. This alone is enough to distort the final result. Note that in programming, 20 and 20.0 are not thesame!
So then, what should you use? Well not many person might know, but whenever you want to return a non-string value with the input() function, the best function to use is eval(). To be used as eval(input("say something" )).

Cheers

It is never wrong to use int with input , it depends on the problem at hand
Re: Can Someone Help Me With A Solution To This Python Problem by julybaba(m): 2:16am On Apr 28, 2020
Hmmm
Re: Can Someone Help Me With A Solution To This Python Problem by farmsata(m): 4:47am On Apr 28, 2020
scarplanet:


if num1 == num2, then num1 cannot be the larger number??

The program will return a value if either of two conditions are met. If num1==num2, this program will print num1 as larger number instead of terminating without results.

elif statement will cater for all conditions. The op is not concerned about that lol. He wants his program to run.
Re: Can Someone Help Me With A Solution To This Python Problem by farmsata(m): 4:57am On Apr 28, 2020
num1 = eval(input("Enter number one " ))

num2 = eval(input("Enter number two " ))

largenum = 0

if num1 > num2:

largenum = num1

elif num1 < num2:

largenum = num2

print(str(largenum) + " is the large number"wink

else:

print("The numbers are equal"wink
Re: Can Someone Help Me With A Solution To This Python Problem by scarplanet(m): 7:22am On Apr 28, 2020
silento:


It is never wrong to use int with input , it depends on the problem at hand


Like I said, you can never tell the behavior and intent of the users of your code. What if someone someday decides to compare floating numbers?

It's best to use the correct syntax, eval()
Re: Can Someone Help Me With A Solution To This Python Problem by scarplanet(m): 7:32am On Apr 28, 2020
farmsata:
num1 = eval(input("Enter number one " ))

num2 = eval(input("Enter number two " ))

largenum = 0

if num1 > num2:

largenum = num1

elif num1 < num2:

largenum = num2

print(str(largenum) + " is the large number"wink

else:

print("The numbers are equal"wink


1. Line 3 is not necessary. Memory space is being wasted initializing largenum = 0.

2. Your code will yield no result If num1 is larger. tongue tongue

1 Like

Re: Can Someone Help Me With A Solution To This Python Problem by silento(m): 10:20am On Apr 28, 2020
scarplanet:


1. Line 3 is not necessary. Memory space is being wasted initializing largenum = 0.

2. Your code will yield no result If num1 is larger. tongue tongue

U seems to know too much about python , instead of correcting someone you are mocking them , don't forget python is not the only programming language someone that has good background from c or c++ will always write his code like that ,
Re: Can Someone Help Me With A Solution To This Python Problem by farmsata(m): 10:59am On Apr 28, 2020
scarplanet:


1. Line 3 is not necessary. Memory space is being wasted initializing largenum = 0.

2. Your code will yield no result If num1 is larger. tongue tongue

scarplanet:


1. Line 3 is not necessary. Memory space is being wasted initializing largenum = 0.

2. Your code will yield no result If num1 is larger. tongue tongue

Hahaha

My code worked fine before I posted. You are right about line 3 being irrelevant. I put it there anyway wink
Re: Can Someone Help Me With A Solution To This Python Problem by scarplanet(m): 12:04pm On Apr 28, 2020
silento:


U seems to know too much about python , instead of correcting someone you are mocking them , don't forget python is not the only programming language someone that has good background from c or c++ will always write his code like that ,


I am sure farmsata understands we are just trying to get under each other's skin; I don't see how I am making fun of him.
If trying to point out something means I am mocking the person, then I'll hold my peace moving forward.

And No, I dont know all of python. No one is a bundle of knowledge.

Cheers
Re: Can Someone Help Me With A Solution To This Python Problem by Nobody: 3:06pm On Apr 28, 2020
jjmk:
I need to know more about input(). I don't seem to understand it clearly.

There are two numbers one higher than the other. Let's say 40 and 20 respectively.

Num1 = int(input("Enter the first number: "wink)
Num2 = int(input("Enter the second number: "wink)

# take the larger number

If num1 > num2:
Larger_num = num1
Else:
Larger_num = num2

# Print the result
Print("The larger number is:" Larger_num)

Pls guys help...I am stuck here.
What is the problem ? Didn't it give u the desired result
Re: Can Someone Help Me With A Solution To This Python Problem by Nobody: 3:12pm On Apr 28, 2020
farmsata:
num1 = eval(input("Enter number one "wink)
num2 = eval(input("Enter number two "wink)
largenum = 0
if num1 >= num2:
largenum = num1
else:
largenum = num2
print(str(largenum) + " is the large number"wink
U don't need to initialize largenum to 0 for it to work
Re: Can Someone Help Me With A Solution To This Python Problem by Nobody: 11:31pm On May 09, 2020
jjmk:
I need to know more about input(). I don't seem to understand it clearly.

There are two numbers one higher than the other. Let's say 40 and 20 respectively.

Num1 = int(input("Enter the first number: "wink)
Num2 = int(input("Enter the second number: "wink)

# take the larger number

If num1 > num2:
Larger_num = num1
Else:
Larger_num = num2

# Print the result
Print("The larger number is:" Larger_num)

Pls guys help...I am stuck here.

How are you faring big man? Hope you have advanced your Python game? Do know that there are people who really want to see you having flawless programming experience. Should you need any help, hit me at +234 813 2329 187. I will be glad to help...

(1) (Reply)

How Should I Learn Javascript / How To Convert Ios App To Android App? / Accepted Into The ALX Software Engineering Program

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 40
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.