This is a code to determine if the numbers in the list are prime or not (python2):
this works (1):
L = [4 , 9, 13, 15, 16]
def primenumber4():
for i in L:
compositenumber = False
for x in range(2,i):
if i%x == 0:
compositenumber = True
break
if compositenumber:
print "%s is not a prime number" % i
else:
print "%s IS a prime number" % i
primenumber4()
But this does not (2):
L = [4 , 9, 13, 15, 16]
def primenumber4():
compositenumber = False
for i in L:
for x in range(2,i):
if i%x == 0:
compositenumber = True
break
if compositenumber:
print "%s is not a prime number" % i
else:
print "%s IS a prime number" % i
primenumber4()
it gives the number 13 as not a prime.
But why?
according to this tread:
Short Description of the Scoping Rules?
"One of the greater surprises to many newcomers to Python is that a for loop does not create a variable scope" in Antti Haapala's answer
so I imagine the value "leaks" into the function scope:
which is the case with a single for loop
example:
def function():
i = 9
k = False
for x in range(2, 9):
if i%x == 0:
k = True
break
print k
which prints k = True so the variable changed
So my question is: Why doesn't the variable "compositenumber" change in (2)?
If a for loop doesn't have scope why isn't the variable a local variable in the function? why doesn't it "leak through twice passing both for loops" and ending up as a local variable in the function def.
I'm trying to understand the python scope rules because I don't think I get it completely. Thanks in advance!
This is not a scope issue. The issue is that when you assign primenumber = False inside the for loop, that assignment happens every time you go through the loop. It resets the value to False every time.
When you assign primenumber = False above the loop, it only gets assigned that way once, so primenumber remains True after the first time it's set to True.
Also, primenumber is a terrible name for a variable that indicates whether a number is composite.
Related
I am a beginner in the field of python coding.I am stuck with this code which I wrote as a solution for the problem -"ambiguous permutations" on codechef.It shows time limit exceeded when I run it on the website.Please help me with this issue.The code is given below:
type=[]
while(True):
n = int(raw_input())
def inverse_calculation(listhere=[]):
listtwo =[]
for i in range(1,n+1):
a = listhere.index(i)
listtwo.append((int(a)+1))
if (listtwo == listhere):
return type.append('ambiguous')
else:
return type.append('not ambiguous')
if (n!=0):
list1 = []
list1 = [int(x) for x in raw_input().split()]
inverse_calculation(list1)
else:
break
for element in type:
print(element)
Here in the code your n value will always be not equal to zero ,that is, your if condition will always be true, come what may, because you are not decrementing or altering the value of n in you entire program. And since your while condition is always true you are bound to get this error.
So my suggestion is you try to alter the value of n in your program.
So I am rather new to programming and just recently started with Classes and we are supposed to make a phonebook that can be loaded in seperate text files.
I however keep running into the problem in this section that when I get into the for-loop. It hits a brick wall on
if storage[2] == permaStorage[i].number:
And tells me "IndexError: list index out of range". I am almost certain it is due to permaStorage starts out empty, but even when I attempt to fill it with temporary instances of Phonebook it tells me it out of range. The main reason it is there is to check if a phone number already exists within the permaStorage.
Anyone got a good tip on how to solve this or work around it?
(Sorry if the text is badly written. Just joined this site and not sure on the style)
class Phonebook():
def __init__(self):
self.name = ''
self.number = ''
def Add(name1, number1):
y = Phonebook()
y.name = name1
y.number = number1
return y
def Main():
permaStorage = []
while True:
print " add name number\n lookup name\n alias name newname\n change name number\n save filename\n load filename\n quit\n"
choices = raw_input ("What would you like to do?: ")
storage = choices.split(" ")
if storage[0] == "add":
for i in range(0, len(permaStorage)+1):
if storage[2] == permaStorage[i].number:
print "This number already exists. No two people can have the same phonenumber!\n"
break
if i == len(permaStorage):
print "hej"
try:
tempbox = Add(storage[1], storage[2])
permaStorage.append(tempbox)
except:
raw_input ("Remember to write name and phonenumber! Press any key to continue \n")
I think problem is that permaStorage is empty list and then u try to:
for i in range(0, len(permaStorage)+1):
if storage[2] == permaStorage[i].number:
will cause an error because permaStorage has 0 items but u trying to get first (i=0, permaStorage[0]) item.
I think you should replace second if clause with first one:
for i in range(0, len(permaStorage)+1):
if i == len(permaStorage):
print "hej"
try:
tempbox = Add(storage[1], storage[2])
permaStorage.append(tempbox)
if storage[2] == permaStorage[i].number:
print "This number already exists. No two people can have the same phonenumber!\n"
break
So in this case if perStorage is blank you will append some value and next if clause will be ok.
Indexing starts at zero in python. Hence, a list of length 5 has the last element index as 4 starting from 0. Change range to range(0, len(permastorage))
You should iterate upto the last element of the list, not beyond.
Try -
for i in range(0, len(permaStorage)):
The list of numbers produced in range() is from the start, but not including the end, so range(3) == [0, 1, 2].
So if your list x has length 10, range(0, len(x)) will give you 0 through 9, which is the correct indices of the elements of your list.
Adding 1 to len(x) will produce the range 0 through 10, and when you try to access x[10], it will fail.
I'm trying to return the factors of a number which are prime numbers in this code. For example, if someone enters in n = 55, they'll get back 5 and 11. I believe I have the first part correct in checking for the factors, but somehow I'm not sure what I'm missing in the second part of checking for prime numbers.
When I run the code for 55, I only get 5 (11 is missing).
Can someone please help review my code and provide feedback?
l = []
primefactors = []
def factor(n):
count = 0
print 'These are the factors:'
for num in range(2,n+1):
if n%num == 0: #checks that it is a factor
print num #These are the factors
l.append(num)
for i in l:
if i == 2 : #checks for two
primefactors.append(i)
else:
for x in range(3,i+1,2): #checks for odd numbers
if i%x == 0:
count += 1
if count == 1:
primefactors.append(i)
print "These are the prime factors:"
print primefactors
You can get all the factors by simply removing all unnecessary stuff, but that will not guaranty, that the number is prime number:
l = []
primefactors = []
def factor(n):
count = 0
for num in range(2,n+1):
if n%num == 0 and num != n: #checks that it is a factor
primefactors.append(num)
print ("These are the prime factors:")
print (primefactors)
del l[:]
del primefactors[:]
try this one, I added an additional check to see if number is a prime before appending to primefactors list.
primefactors = []
def factor(number):
for i in range(2,int(number//2)+1):
prime = True
for j in range(2,int(i**.5)+1):
if i%j == 0:
prime = False
break
if prime and number%i == 0:
primefactors.append(i)
print (primefactors)
factor(92)
factor(55)
output:
[2, 23]
[2, 23, 5, 11]
I need to get user input to generate a list of 8 numbers, but when they input a number that is already in the list print and error . Without using the in function to determine if its in the list. Here's what I have so far.
def main():
myList = range(9)
a= True
for i in myList:
while a == True:
usrNum = int(input("Please enter a number: "))
if usrNum != myList[i]:
myList.append(usrNum)
print(i)
main()
Error for above code,
Scripts/untitled4.py", line 18, in main
myList.append(usrNum)
AttributeError: 'range' object has no attribute 'append'
The issue seems to be your way of generating myList. If you generate it with myList = [range(9)] you'll get:
[[0, 1, 2, 3, 4, 5, 6, 7, 8]]
Try using simply:
myList = range(9)
Also, you need to change myList.append[usrNum] with myList.append(usrNum) or you'll get a:
TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'
You could also use wim's suggestion instead of the != operator:
if myList.__contains__(usrNum):
myList.append(usrNum)
There are two ways you can go about this:
Loop through the list to check each element.
The in operator is effectively doing:
for each value in the list:
if the value is what you're looking for
return True
if you reach the end of the list:
return False
If you can add that check into your code, you'll have your problem solved.
Use an alternate way of tracking which elements have been added
Options include a dict, or bits of an int.
For example, create checks = {}. When you add an value to the list, set checks[usrNum] = True. Then checks.get(usrNum, False) will return a boolean indicating whether the number already exists. You can simplify that with a collections.DefaultDict, but I suspect that may be more advanced than you're ready for.
The first is probably the result your instructor is after, so I'll give you a simple version to work with and massage to fit your needs.
myList = []
while True:
usrNum = int(input())
found = False
for v in myList:
if usrNum == v:
found = True
if not found:
myList.append(usrNum)
else:
#number was already in the list, panic!
Most instructors will be more impressed, and hence award better grades, if you can figure out how to do something like method 2, however.
You could do something like this, modify as needed (not sure when/if you want to break when the user enters a number that is already in the list, etc.)
This prompts for user input until they enter an item that already exists in the list, then it prints a message to the user, and stops execution.
def main():
mylist = range(9)
while True:
usrNum = int(input("Please enter a number: "))
if existsinlist(mylist, usrNum):
print("{} is already in the list {}".format(usrNum, mylist))
break
else:
mylist.append(usrNum)
def existsinlist(lst, itm):
for i in lst:
if itm == i:
return True
return False
Perhaps the point of this homework assignment is to help you understand how an operator like in is more efficient to read (and write, and compile) than the explicit loop that I used in the existsinlist function.
Not sure if list-comperehension would be allowable in this case, but you also could've done something like this, without relying on the existsinlist helper function:
def main():
mylist = range(9)
while True:
usrNum = int(input("Please enter a number: "))
if [i for i in mylist if i == usrNum]:
print("{} is already in the list {}".format(usrNum, mylist))
break
else:
mylist.append(usrNum)
In this case, the result of the list-comprehension can be evaluated for truthiness:
An empty list like [] results if no matching value exists, and this will be considered False
A non-empty list will result if at least one matching value exists, and this will be considered True
Yet another option which short-circuits and may be preferable:
if any(usrNum == i for i in mylist)
Ok, so, this is the longest code I've ever written, so I apologize if it's a bit messy. First computer science assignment ever.
def main():
#generate random value
import random
rand = random.randint(1, 99)
#instructions
print("""The purpose of this exercise is to enter a number of coin values
that add up to a displayed target value. Enter coins as 1=penny, 5-nickel,
10-dime, 25-quarter. Hit return after the last entered coin value.""")
#defining function for 'first coin'
def firstCoin ():
coins = []
global coins
coin = int(input("Enter first coin: "))
if coin > rand:
print("Sorry, total value exceeds", rand, "cents.")
again = input("Try again (y/n): ")
if again == "y" or "Y":
main()
else:
sys.exit() #ends program
elif coin in possible:
coins.append(coin)
nextCoinplease()
#defining function for 'next coin'
def nextCoinplease ():
while True:
nextcoin = (input("Enter next coin: "))
if nextcoin in possible:
coins.append(nextcoin)
elif nextcoin == "":
break
else: print("Invalid entry.")
#making lists
possible = [1, 5, 10, 25]
print("Enter coins that add up to", rand, "cents, one per line.") #program start
firstCoin ()
sumcoin = sum(coins)
print(sumcoin)
if sumcoin == rand:
print("Correct!")
else:
print("Invalid entry.")
firstCoin()
main()
So, this is my issue. For some reason, user input in the function nextCoinplease does not get added to the list "coins", only the first input from the function firstCoin does. This means that sum(coins) is literally only the first input. I cannot for the life of me figure out why, so any input would be greatly appreciated, thanks!
You have two input statements, one to get coin and one to get nextcoin. They are different. What's the difference?
(I'm deliberately not giving the answer outright because from what you've written so far, I am sure you can figure this one out given this hint.)
The return type of input is a string, so nextcoin in possible always fails since possible only contains integers. Try using int() to parse the input as an integer.
Your code never gets to check if the rand is equal to sumCoin, so it never stopped. I've fixed the problem, this code works now.
Demo on repl.it
What did I do?
I moved your if statement that checked if rand == sumCoin at the beginning of the while loop in nextCoinPlease(), so that it will check the sumCoin value before entering each next coin value and will stop once it equals rand.
Code:
import random
import sys
def main():
rand = random.randint(1, 99)
print('''The purpose of this exercise is to enter a number of coin values \
that add up to a displayed target value. Enter coins as 1=penny, \
5=nickel, 10-dime, 25-quarter. Hit return after the last entered \
coin value.''')
coins = []
def firstCoin():
coin = int(input("Enter first coin: "))
if coin > rand:
print('Sorry, total value exceeds ', rand, ' cents.')
again = input('Try again? (y/n): ')
if again == 'y' or 'Y':
main()
else:
sys.exit()
elif coin in possible:
coins.append(coin)
nextCoinPlease()
def nextCoinPlease():
while True:
sumCoin = sum(coins)
print('Total Value: ' + str(sumCoin))
print ''
if sumCoin == rand:
print('Correct! You Win!')
sys.exit()
elif sumCoin > rand:
print('You exceeded the total value! You lose! Try again!')
sys.exit()
nextCoin = (input('Enter next coin: '))
if nextCoin in possible:
coins.append(nextCoin)
elif nextCoin == "":
break
else:
print('Invalid entry.')
possible = [1, 5, 10, 25]
print('Enter coins that add up to', rand, 'cents, one per line.')
firstCoin()
main()