I have a function that takes a set, prints the min/max, and then asks the user to remove items in the set until the set is empty.
My code:
def deleteFromNumSet(numsSet):
while len(numsSet)!=0:
print("Max is",max(numsSet),"and Min is", min(numsSet))
num=input("Enter a number between the two.")
if num in numsSet:
print("The number is in the set.")
numsSet.remove(num)
else:
print("The number is not in the set.")
return("No more numbers left in the set.")
The code will say that the "number is not in the set", regardless of whether or not it actually is in the set. It worked using the emulator on repl.it (which was where I coded it originally), but it does not work on my Python program (I am currently using version 3.4.1). I would like to know why it worked on the (older) version of python but does not work now, and a solution that would work for current versions of python.
input() returns a string, not an integer. If our set contains integers then Python will not consider the string equal to the numbers.
Convert the input to an integer first:
num = int(input("Enter a number between the two."))
In Python 2.7, input() is a different function; it essentially does the same as eval(input()) would do in Python 3. As such it automatically interprets digits as a Python integer literal.
To make your code work in both Python 2 and 3 would require a lot more experience with both versions. See the Porting Python 2 Code to Python 3 how-to if you really want to go this way.
Related
Can anyone please help me how to find the largest integer from given list using Robot framework.
${list} = [2,45,32,55,332,5,5]
I want the highest number to be returned as my output.
Assuming ${list} has a valid python list of numbers, you can use the evaluate keyword to call python's max function to easily find the largest value.
The following example assumes that ${list} is an actual python list of integers, and uses robot's special syntax for converting robot variables python variables (eg: $list).
${max}= Evaluate max($list)
Should be equal as numbers ${max} 332
Say for example,
>>> a=5
>>> b=10
>>> if a!=0 and b:print 'Hello'
Hello
The above snippet works fine on python console in every other os and online interpreters.!
Now on removing the space after a!=0 and logical and, on python console in Mac or in any other online interpreters for python like TIO.
>>> a=5
>>> b=10
>>> if a!=0and b:print 'Hello' # note the difference
Hello
PFB the screenshot!
But the same doesn't seem to work on a python console in linux and windows and also in IDE like Eclipse with pydev plugin installed(Mars)
It'd be great if anyone could
throw light on this behaviour!
is it really pythonic to use the second snippet?
Note: realised this while working on few challenges in code golf-PPCG where the space really matters!
P.S. please correct me if I'm wrong!
If you're code golfing, go for it. It's terrible practice in normal code, but code golf throws out all standards of normal code quality anyway.
Python splits the 0 from the and in if a!=0and b much the same way as it splits the != from the 0 or the a from the !=. The source code gets fed through a tokenizer that splits it into elements known as tokens. Python's tokens include such things as number literals (12, 1.2, 1e4, 0xabc, etc.), names (foo, eggs, steve, etc.), keywords (if, and, while, etc.), and so on. When the tokenizer hits this point:
if a!=0and b
^
it sees that the longest valid token starting at the 0 is just 0 - it can't continue on and include the a or anything after that, because that doesn't fit any possible token format. Thus, it splits the 0 from the and.
a = (random.random(), random.random())
print(a)
print(a[0])
the result is:
(0.4817527913069962, 0.7017598562799067)
0.481752791307
What extra is happening behind printing a tuple(similar behavior for list)? Why is there extra fraction?
Thanks a lot.
BTW, this is python 2.7
What you are seeing is the difference between the formatting choices made by str(float) and repr(float). In Python 2.x, str(float) returns 12 digits while repr(float) returns 17 digits. In its interactive mode, Python uses str() to format the result. That accounts for the 12 digits of precision when formatting a float. But when the result is a tuple or list, the string formatting logic uses repr() to format each element.
The output of repr(float) must be able to be converted back to the original value. Using 17 digits of precision guarantees that behavior. Python 3 uses a more sophisticated algorithm that returns the shortest string that will round-trip back to the original value. Since repr(float) frequently returns a more friendly appearing result, str(float) was changed to be the same as repr(float).
I am new to python. I am using python 2.7 version I have two unicode string variables,
a = u'0125', b = u'1234'
Now i want to convert this variables into integer and append it into a List like [0125, 1234]. This is my expected output.
I have tried to convert these variables into integer and appended it into the List and got the output as [125, 1234]. Preceeding zero is missing in that value. Can someone give better solution for this?.
My program continues to tell the user to guess lower, even if the user types in the correct number. How do I solve this?
import random
a=raw_input('enter a number')
b= random.randrange(0,11)
while a!=b:
if a < b:
print ('you are not correct, try a higher number')
if a > b:
print('you are not correct, try a lower number')
if a== b:
print('wwcd')
print b
The problems with it as currently are that a is never updated & that you are using 'a' the character rather than a the variable
while `a`!=b:
Compares a constant character to the number b (and will always be larger). It should be:
while a!=b:
This change should be applied to all your conditional statements (also it's probably best to remove the repeated if 'a'== b blocks, as only one is needed)
For the next part you need to update a as part of the loop (such that the user can change the input). You only need to move the part where you assign a a value downwards:
while a!=b:
a=raw_input('enter a number')
//rest of your conditionals statements
EDIT:
You have a 3rd problem. The raw_input() function returns a string and you need an int for comparison. To fix it simply cast it to int: int(raw_input('Enter a number')) or, more appropriately use Python 2.x's input() function. This will evaluate anything you input so will return int when you enter a number. But watch out, Python 3.x input() acts like raw_input() in 2.x and raw_input() is gone.