Jump to content

I'm learning Python, and have been for a few weeks now, the thing with me is that I cannot move on unless I understand every single bit of code that I type. With that said can somebody please explain this line to me (more specifically the functionality/meaning of the % signs).

 

guess=raw_input("Guess a number from 0 to %d:" %highest)

Link to comment
https://linustechtips.com/topic/332037-python-help/
Share on other sites

Link to post
Share on other sites

I'm learning Python, and have been for a few weeks now, the thing with me is that I cannot move on unless I understand every single bit of code that I type. With that said can somebody please explain this line to me (more specifically the functionality/meaning of the % signs).

 

guess=raw_input("Guess a number from 0 to %d:" %highest)

 

I would not take this as definitive but I have also been learning python and my lectureer has said that there are 3 types of division each with their own opperator.

/ will return a float

// will return a int

% will return the modulus of a number

Examples 

12 / 7 will return 1.714

12 // 5 will return 1

12 % 7 will return 5 (7 goes into 12, once, with 5 left over)

if this is how % works in context to your code, I am unsure, but that is how it worths when dealing with in's/float's

side note, update to python 3.3/3.4 since you're using raw_input I know that it's 2.7 which is outdate with outdated stuff like that, in 3.3 raw_input is just input 

Link to comment
https://linustechtips.com/topic/332037-python-help/#findComment-4507527
Share on other sites

Link to post
Share on other sites

I would not take this as definitive but I have also been learning python and my lectureer has said that there are 3 types of division each with their own opperator.

/ will return a float

// will return a int

% will return the modulus of a number

Examples 

12 / 7 will return 1.714

12 // 5 will return 1

12 % 7 will return 5 (7 goes into 12, once, with 5 left over)

side note, update to python 3.3/3.4 since you're using raw_input I know that it's 2.7 which is outdate with outdated stuff like that, in 3.3 raw_input is just input 

Thanks for the help, the guy im learning off of is using python 2.7 so I thought it would be good to use the same version

Link to comment
https://linustechtips.com/topic/332037-python-help/#findComment-4507541
Share on other sites

Link to post
Share on other sites

Someone correct me if I'm wrong, but I think in the context of the code you provided the "%" is being used to format input and output, not as a mod operator.

 

Yeah it is used to replace the %d with "%highest" and the the "d" signifies that this should be a digit. 

Yeah, you both are correct, I figured that out after further research.... it is so simple, I feel dumb :(

Link to comment
https://linustechtips.com/topic/332037-python-help/#findComment-4508065
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

×