trying to generate math questions, comparissons not working - python

can anyone help me? im pretty new to python and im trying to generate 10 files, each with increasingly harder questions. this code is for difficult 2. I dont want the answers in dif. 2 to be negative so whenever i get a second number bigger than the first i swap the two. for some reason some of them still come out with the first number bigger than the second. i added the "its less than" print statments for testing and it will detect the fact that its less than but wont do something about it.

Your issue is that you're casting your random numbers to a string before comparing their mathematical values. You need to compare them as integers then cast them to strings.

I believe this is because you are checking for comparison between 2 strings not 2 integers. This will give bad results for this type of program
num1 = str(r.choice(numbers))
num2 = str(r.choice(numbers))
Here you are storing strings and not integers.
and then below this you are checking if num1 <= num2.
Convert them to integers before comparing them and your code should work.

Related

Why can't a long integer be converted to an integer when inside a Python list?

I have seen many posts here, which gives ways of removing the trailing L from a list of python long integers.
The most proposed way is
print map(int,list)
However this seems not to work always.
Example---
A=[4198400644L, 3764083286L, 2895448686L, 1158179486, 2316359001L]
print map(int,A)
The above code gives the same result as the input.
I have noticed that the map method doesn't work whenever the number preceding L is large, and only when the numbers are in a list. e.g. Application of int() on 4198400644L does give the number without L, when out of the list.
Why is this occurring and more importantly, how to overcome this?
I think I really need to remove this L, because this is a small part of a program where I need to multiply some integer from this list A, with some integer from a list of non-long integers, and this L is disturbing.I could ofcourse convert the long integers into string,remove the L and convert them back to integer.But is there another way?
I am still using the now outdated Python 2.7.
Python has two different kinds of integers. The int type is used for those that fit into 32 bits, or -0x80000000 to 0x7fffffff. The long type is for anything outside that range, as all your examples are. The difference is marked with the L appended to the number, but only when you use repr(n) as is done automatically when the number is part of a list.
In Python 3 they realized that this difference was arbitrary and unnecessary. Any int can be as large as you want, and long is no longer a type. You won't see repr put the trailing L on any numbers no matter how large, and adding it yourself on a constant is a syntax error.

How to change numbers in a number

I'm currently trying to learn python.
Suppose there was a a number n = 12345.
How would one go about changing every digit starting from the first spot and iterating it between (1-9) and every other spot after (0-9).
I'm sadly currently learning python so I apologize for all the syntax error that might follow.
Here's my last few attempts/idea for skeleton of the code.
define the function
turn n into string
start with a for loop that for i in n range(0,9) for i[1]
else range(10)
Basically how does one fix a number while changing the others?
Please don't give solution just hints I enjoy the thinking process.
For example if n =29 the program could check
19,39,49,59,69,79,89,99
and
21,22,23,24,25,26,27,28
Although you are new, the process seems far easy than you think.
You want to make that change to every digit of the number (let's say n=7382). But you cannot iterate over numbers (not even changing specific digits of it as you want to): only over iterables (like lists). A string is an iterable. If you get the way to do a int-str conversion, you could iterate over every number and then print the new number.
But how do you change only the digit you are iterating to? Again, the way is repeating the conversion (saving it into a var before the loop would make great DRY) and getting a substring that gets all numbers except the one you are. There are two ways of doing this:
You search for that specific value and get its index (bad).
You enumerate the loop (good).
Why 2 is good? Because you have the real position of the actual number being change (think that doing an index in 75487 with 7 as the actual one changing would not work well when you get to the last one). Search for a way to iterate over items in a loop to get its actual index.
The easiest way to get a substring in Python is slicing. You slice two times: one to get all numbers before the actual one, and other to get all after it. Then you just join those two str with the actual variable number and you did it.
I hope I didn't put it easy for you, but is hard for a simple task as that.

int() vs float() inputs in Python 3 and their utility?

This doesn't refer to a specific code of mine, so I hope this does not defy community standards for asking questions. I'm still learning, so please let me know if this kind of question is inappropriate for future reference!
I am trying to gain a thorough understanding of the utility of certain commands as I embark on learning to use Python 3. I have never coded before, so I do not have background in any other language. I was hoping someone could help me understand this more thoroughly.
Basically, I understand that when prompting for a user input with a numeric value, it is sometimes correct to write float(input()), and sometimes correct to write int(input()). I know in mathematics that an integer is a whole number, and a floating point number is any number defined with a whole portion, a radix, and a mantissa (like 4.32). I don't understand the utility of converting a user input to one or the other.
For example, if I write int(input("Input a decimal. ")) and the user inputs 4.3, the program will return a value error. What is the utility in this? So:
What is the utility in converting an input() to float() or int()?
I understand when I would want an integer (e.g; if I want the user to input how many times to multiply a particular number by itself), but why would I want a floating point input?
In general, when do I need to implement either, and how can I recognize which one a program needs?
Aside from user input, in what other general cases would I want to implement either command?
If anyone has any additional reading on how and when to convert certain defined variables or inputs, please send them my way!
EDIT:
Here is an example of a code that I wrote that I think highlights my confusion about if and when to use int() and float():
price=input("How much did the item cost?: $")
if float(price)<0:
print("The price cannot be negative.")
else:
price=int(float(price)*100)
paid=input("How much did the customer pay?: $")
paid=int(float(paid)*100)
Did I do this correctly? The larger program of which this is a part works fine, but I'm not sure if I added unnecessary command or implemented the commands correctly.
Thank you so much for your help!
Naomi
It has nothing about utility, it has to do with what are the possible range of values you're program should/needs to accept.
If it needs to accept both integers and floats as inputs, then you should convert to float since floats can represent the integers.
But if you're program requires that the input be specifically an integer, then you should be casting to int.
EDIT:
In your example, you should always be using float, since money has a decimal value.
If you were asking "How many bananas did you buy?" You'd want to convert to int since those values are going to be 0, 1, 2, 3, 4, .... And then when you ask "How much did you pay for these bananas?" You'd want to convert to float since those inputs can range from 3.15, .77, 1, 1.00, ... etc.
So that you can work with numbers. You can't very well multiply '3' by '2' in Python.
So that you can work with floating-point numbers. Some things in life can come in bits and pieces, like kilograms, seconds, or grade point averages.
If your program needs to work with the numbers between consecutive integers, you should probably use float.
If you use strings in your program, you may want them to be numbers, regardless of where the strings came from.
You'll need to evaluate this on a case-by-case basis.
You can implement error checking using the try function. The only reason to use an int vs a float, would be if you are trying to save memory, in the case of embedded ROM where you are severly limited.
In my opinion, you should always use float whenever you're not sure because it accepts more values so it will less likely return an error.
Unless your code expects a rounded number (you can round the number if it's a float but can cause confusion if done incorrectly) from a users input.
Bad example:
username_input = float(input("Enter your username: "))
pincode_input = int(input("Enter pincode: "))
pincode = 1234
if pincode == pincode_input:
print("Good")
else:
print("Bad")
In this case, the code expects the user to input a rounded number so I like to use int() so that if the user inputs a decimal number then I know where the problem is and can fix it directly instead of fixing every other code that relies on it.
This is a bad example because the code does run if the user's input doesn't get converted with int() but I hope you get the idea.

Small == Equal while creating simple python code

I'm completely new to Python so all of this is a bit new for me.
I'm trying to create a script in which it by itself thinks of a number and after wards wants the user to guess a number and enter it. It seemed to work but there are wo problems here.
It understands everything however it does not understand the == part.
Because even though the both numbers are the same, it still goes on towards the else statement.
import random
while True:
Thethinker = random.randint(1,10)
Guessnumber = (input("Guess a random number "))
if Guessnumber == Thethinker:
print("Correct")
print(Thethinker)
else:
print("Not correct")
print(Thethinker)
Any help?
Assuming Python 3, input returns a string.
In Python, a string and a number can never be "equal"; you have to transform one or the other. E.g, if you use
Thethinker = str(random.randint(1,10))
then the equality-comparison with another string can succeed.
Your input returns a string instead of an integer. To get an integer do:
int(Guessnumber)
This is better than using str() because then if you ever want to change the numbers, you wouldn't be able to do that without doing the method I suggested
As far as I know, I don't do much Python

Python: Compare a list to a integer

Ok, no i dont believe this is a repeat question of the other ones on here. Here is what i am trying to do (I am new to Python and self teaching so bear with me).
I have a set of data that is a length of 3. This set makes up a hex value i.e. 0,9,9f is really just the hex value 99f.
I want to take that data set and compare it to an integer that i have (2463). I know there is a decimal to hex converter but how do i combine the data set or split apart the integer value to be able to compare the two to make sure they are equal?
Where your list is 3 elements containing '99f' - the following returns 2463
int(''.join(your_list), 16)

Categories

Resources