Having problems with my code and cannot seem to fix it or know where i have gone wrong. any help will be appreciated.
it runs but doesn't go any further than the user inputting the text file name.
it is supposed to read the user input and from that read a text file and declare whether it is a magic puzzle or not, i wanted this to read 5x5 as well but i'm a bit lost on how to do it
column = 0
row = 0
data = []
def main():
file = input("Enter filename :")
while True:
try:
f = open(file+".txt","r")
break
except:
file = input("Enter filename :")
for line in f.readline():
numbers = line.split(' ')
cube = [int(x) for x in numbers]
is_magic(x)
def is_magic(x):
if not dupe(x) and check_sum(x):
print ('Valid')
else:
print ('Invalid')
def dupe(x):
if len(x) == len(set(x)):
return False
return True
def check_sum(x):
if vertical_check(x) and horizontal_check(x) and diagonal_check(x):
return True
return False
def vertical_check(x):
if sum(x[0:9:3]) == sum(x[1:9:3]) == sum(x[2:9:3]) == 15:
return True
return False
def horizontal_check(x):
if sum(cube[0:3]) == sum(cube[3:6]) == sum(cube[6:9]) == 15:
return True
return False
def diagonal_check(x):
if sum(cube[0:9:4]) == sum(cube[2:7:2]) == 15:
return True
return False
def writeFile(x):
f = open("VALID_"+x+".txt","w")
text = ""
for a in data:
for x in a:
text = text+str(x)+" "
text = text+"\n"
f.write(text)
f.close()
return
main()
'''
txt file 3x3
2 9 4
7 5 3
6 1 8
The break statement inside while True makes you break the loop.
while True:
try:
f = open(file+".txt","r")
break
the reason for the program being stuck is the break statement after opening the file. This break statement will exit the while loop, so the rest of the code will not be executed.
f = open(file+".txt","r")
break # <<<< remove this
Related
I am trying to create a function that returns a 4 digit string which consists of 4 unique values.
I currently have the following code:
def generateNum():
ValidNum = False
while ValidNum == False:
RanNumber = random.randint(1000, 9999)
RanNumber = str(RanNumber)
for number in RanNumber:
if RanNumber.count(number) > 1:
ValidNum = False
else:
ValidNum = True
return RanNumber
print(generateNum())
Can someone explain what is wrong with this piece of code and what I can potentially do to fix it?
Thank you.
Following your logic, this is what would have worked:
def generateNum():
ValidNum = False
while True:
RanNumber = random.randint(1000, 9999)
RanNumber = str(RanNumber)
for number in RanNumber:
if RanNumber.count(number) == 1:
ValidNum = True
continue
else:
ValidNum = False
break
if not ValidNum:
continue
break
return RanNumber
import random
def generate_random():
num = ''
while len(num) < 4:
random_int = random.randint(0,9)
if str(random_int) in num:
pass
else:
num += str(random_int)
return num
print(generate_random())
This should work fine
I am trying to make a random word/phrase generator that is like the one that bitwarden has (in python3). But the issue I am running into and need some help with is the addition of 1 number at the end of 1 of the words that is shown.
Something like this Referee-Outrank-Cymbal-Cupping-Cresting-Fiber7-Expensive-Myth-Unveiling-Grasp-Badland-Epiphany-Simplify-Munchkin-Pastrami-Spiffy-Gladly-Skeptic-Retouch-Buckskin
What is very important here is that the number is "random" and the word it is attached to is "random".
Code I have written so far:
Word list I am using is https://svnweb.freebsd.org/csrg/share/dict/words?view=co&content-type=text/plain but without ' in any of the words.
#pycryptodome==3.15.0
from Crypto.Random import random
import beaupy
import os
def clear():
os.system('clear||cls')
def main():
while True:
try:
number = int(beaupy.prompt("How many words?: "))
except ValueError as e:
print(f'Oops! Something went wrong.\nError: {e}\n\n')
input('Press "enter" to continue...')
clear()
continue
if number > 20 or number < 3:
print("20 words is the maximum number of words you can use. And 5 words is the minimum.\n\n")
input('Press "enter" to continue...')
clear()
else:
break
cwd = os.getcwd()
word_path = f"{cwd}/words.txt"
with open(word_path, 'r') as fh:
words = fh.read().lower()
word_list = words.splitlines() #list of words
sep = beaupy.prompt('Line separator? (leave empty for default "-"): ')
if sep == '' or sep == ',':
sep = '-'
#Returns True or False. Basically Yes or No?
if beaupy.confirm("Capitalize?"):
"""Make list of words with the first letter capitalized."""
c_lst = []
for i in word_list:
c_lst.append(i.title())
capital_words = f'{sep}'.join(random.choice(c_lst) for _ in range(number))
else:
default_words = f'{sep}'.join(random.choice(word_list) for _ in range(number))
if beaupy.confirm("Number?"):
rn_num = random.randint(0, 9) # <-- Get a random number to be used with only 1 of the words defined in capital_words or default_words below.
#I don't know what to do here... but I need to have a version with the number and one without. (default)
if __name__ == '__main__':
clear()
main()
I am not exactly familiar with string manipulation and searching for answers online just isn't giving me any help with the very specific thing I'm trying to do. All I want is for 1 word in the resulting string to have a "random" number attached to it.
I don't know if I need to re order my code and have it be done a different way. I am having such a headache with this. Any help would be great.
Edit#1
Additional and unrelated note, If anyone knows of a better word list to use, please let me know!
If I am understanding correctly, here is a solution:
#Returns True or False. Basically Yes or No?
capital_words = ''
default_words = ''
if beaupy.confirm("Capitalize?"):
"""Make list of words with the first letter capitalized."""
c_lst = []
for i in word_list:
c_lst.append(i.title())
capital_words = f'{sep}'.join(random.choice(c_lst) for _ in range(number))
else:
default_words = f'{sep}'.join(random.choice(word_list) for _ in range(number))
if beaupy.confirm("Number?"):
rn_num = random.randint(0, 9) # <-- Get a random number to be used with only 1 of the words defined in capital_words or default_words below.
#I don't know what to do here... but I need to have a version with the number and one without. (default)
word_index = random.randint(0, number - 1) # Get random index that is in the word list
if default_words != '':
word_with_number = default_words.split(sep)
else:
word_with_number = capital_words.split(sep)
word_with_number[word_index] = word_with_number[word_index] + str(rn_num)
word_with_number = sep.join(word_with_number)
print(word_with_number)
if default_words != '':
print(default_words)
else:
print(capital_words)
OUTPUT:
detroit-elide-windbag-purge-tort-mortician-codex7-annex-fairy-suntanning
detroit-elide-windbag-purge-tort-mortician-codex-annex-fairy-suntanning
With some help from AnonymousFrog. I was able to get my code working.
The following is the now working code.
from Crypto.Random import random
import beaupy
import os
def clear():
os.system('clear||cls')
def main():
while True:
try:
number = int(beaupy.prompt("How many words?: "))
except ValueError as e:
print(f'Oops! Something went wrong.\nError: {e}\n\n')
input('Press "enter" to continue...')
clear()
continue
if number > 20 or number < 3:
print("20 words is the maximum number of words you can use. And 5 words is the minimum.\n\n")
input('Press "enter" to continue...')
clear()
else:
break
cwd = os.getcwd()
word_path = f"{cwd}/words.txt"
with open(word_path, 'r') as fh:
words = fh.read().lower()
word_list = words.splitlines() #list of words
sep = beaupy.prompt('Line separator? (leave empty for default "-"): ')
if sep == '' or sep == ',':
sep = '-'
#Returns True or False. Basically Yes or No?
capital_words = ''
default_words = ''
if beaupy.confirm("Capitalize?"):
"""Make list of words with the first letter capitalized."""
c_lst = []
for i in word_list:
if len(i) < 3 or len(i) > 9:
pass
else:
c_lst.append(i.title())
cap = True
capital_words = f'{sep}'.join(random.choice(c_lst) for _ in range(number))
else:
cap = False
default_words = f'{sep}'.join(random.choice(word_list) for _ in range(number))
if beaupy.confirm("Number?"):
num = True
rn_num = random.randint(0, 9) # <-- Get a random number to be used with only 1 of the words defined in capital_words or default_words below.
word_index = random.randint(0, number - 1) # Get random index that is in the word list
if default_words != '':
word_with_number = default_words.split(sep)
else:
word_with_number = capital_words.split(sep)
word_with_number[word_index] = word_with_number[word_index] + str(rn_num)
word_with_number = sep.join(word_with_number)
else:
num = False
if cap == True and num == False:
print(capital_words)
if cap == False and num == False:
print(default_words)
if num == True:
print(word_with_number)
Thanks for the help!
(if anyone knows of a better word list to use, feel free to let me know)
I'm having some issues with the psets 2 of cs50p, precisely I'm talking about the "Vanity Plates" problem, where I fulfilled all requests except one, which said:
“Numbers cannot be used in the middle of a plate; they must come at the end. For example, AAA222 would be an acceptable … vanity plate; AAA22A would not be acceptable. The first number used cannot be a ‘0’.” Can you help me? Thank's
this is the code I wrote so far:
def main():
plate = input("Plate: ")
if is_valid(plate):
print("Valid")
else:
print("Invalid")
def is_valid(s):
if s.isalnum() | s[:2].isalpha() | 2 < len(s) < 6 | :
else:
return False
main()
you have to consider all the cases one by one, this is how I solved it:
def main():
plate = input("Plate: ")
if is_valid(plate):
print("Valid")
else:
print("Invalid")
def is_valid(s):
if len(s) < 2 or len(s) > 6:
return False
elif not s[0].isalpha() or not s[1].isalpha():
return False
elif checkFirstZero(s):
return False
elif checkMiddleZero(s):
return False
elif last(s):
return False
elif worng(s):
return False
return True
def last(s):
isAlp = False
isNum = False
for w in s:
if not w.isalpha():
isNum = True
else:
if isNum:
return True
return False
def checkCuntuNNumber(s):
isFirstTry = True
isNum = False
for w in s:
if not w.isalpha():
if isFirstTry:
isNum = True
isFirstTry = False
if isNum and s[-1].isalpha():
return True
def checkMiddleZero(s):
isFirstTry = True
isNum = False
for w in s:
if not w.isalpha():
if isFirstTry:
isNum = True
isFirstTry = False
if isNum and s[-1].isalpha():
return True
else:
return False
def checkFirstZero(s):
for w in s:
if not w.isalpha():
if int(w) == 0:
return True
else:
return False
def worng(s):
for w in s:
if w in [" ", ".", ","]:
return True
return False
main()
This is how I did it. I am sure there is an easier way to do it out there but hopefully this helps :)
characters = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
numbers = ['1','2','3','4','5','6','7','8','9','0']
def main ():
plate = (input ("Plate: ")).upper()
if is_valid(plate):
print ('Valid')
else:
print ('Invalid')
def is_valid (s):
#Check whether length is between 2 and 6 included
if len(s) < 2 or len(s) > 6:
return False
elif char_check(s):
return False
elif char_start(s):
return False
elif zero_check(s):
return False
elif alpha_follow_check (s):
return False
else:
return True
#Check for valid characters
def char_check(s):
for i in s:
if not (i in characters or i in numbers):
return True
#Check whether first two are letters
def char_start (s):
for i in s[:2]:
if not i in characters:
return True
#Check if zero is first number listed
def zero_check (plate_response):
length_string = len (plate_response)
letter_position = 0
number_present = 0
zero_position = None
if any (i in numbers for i in plate_response):
for i in plate_response [0:length_string]:
if i == '0':
zero_position = letter_position
break
letter_position = letter_position + 1
for i in plate_response [0:zero_position]:
if i in numbers:
number_present = 1
if number_present == 0:
return True
else:
return False
#Check alphabet follows numbers
def alpha_follow_check (plate_response):
length_string = len (plate_response)
letter_position = 0
number_position = None
if any (i in numbers for i in plate_response):
for i in plate_response [0:length_string]:
if i in numbers:
number_position = letter_position
break
letter_position = letter_position + 1
for i in plate_response [number_position:length_string]:
if i in characters:
return True
else:
return False
main ()
idk if will help, but the part that i've had the most difficulty in this problem was: "Numbers cannot be used in the middle of a plate; they must come at the end, AAA22A would not be acceptable", then i learned that you can create a full list from the plate that the user inputed, and how to actually use it, with the:
ls = list(s)
for i in range(len(ls)):
After that, we check when the first number appears. "if == '0'" ,then returns False to the function.
After that, if the first number isn't a 0, the program checks if the next item in that list is letter, and, if it is, also return False.
i < len(ls) -1 => this part guarantee that the program will not run in the last item of the list
ls[i+1].isalpha() => and this part check that, if the item on the list was a number, and then the next item is a letter, it returns False
I hope it helps someone, i've spend a lot of time trying to figure it out what to do, and then reached this solution: "for i in range(len(ls))".
Now my code is complete and working.
My code:
def main():
plate = input("Plate: ")
if is_valid(plate):
print("Valid")
else:
print("Invalid")
def is_valid(s):
if not s.isalnum():
return False
elif len(s) < 4 or len(s) > 7:
return False
elif s[0].isdigit()or s[1].isdigit():
return False
elif s[-1].isalpha() or s[-2].isalpha():
return False
else:
ls = list(s)
for i in range(len(ls)):
if ls[i].isdigit():
if ls[i] == '0':
return False
elif i < len(ls) -1 and ls[i+1].isalpha():
return False
else:
return True
main()
I have to read the input and check if the number is actually a float or the string STOP. When I execute my program without the two commented out lines, the result is like this:
1.1
g=nan r=nan% s=nan
2.2
g=nan r=nan% s=nan
The code:
def CheckFloat(number):
try:
float(number)
return True
except ValueError:
return False
def read_input(period):
i = 0
info = []
nbr = []
diff = []
while True:
try:
# if not CheckFloat(input()) and input() != "STOP":
# exit(84)
info.append(input())
if info[i] == "STOP":
fnc_tendency(period, nbr)
else:
nbr.append(float(info[i]))
if i >= 0:
diff.append(nbr[i] - nbr[i - 1])
print_res(nbr, period, diff, i)
i += 1
except(EOFError, StopIteration):
exit(84)
But when I uncomment the two lines
# if not CheckFloat(input()) and input() != "STOP":
# exit(84)
the result looks like this:
1.1
2.2
g=nan r=nan% s=nan
I lose one line of printing, and I don't know why. Could someone help me through this please?
Your float function can be simplified quite a bit
def CheckFloat(n):
return type(n) == float
Otherwise, it looks like you're checking for input twice?
if not CheckFloat(input()) and input() != "STOP":
exit(84)
info.append(input())
# I think you intended to do something more like this:
val = input()
if CheckFloat(val) and val != "STOP":
# do something
else:
# do something
I have a binary search to search a list of emails from a user input. However when the user inputted email isnt found in the list I want the user to be able to enter another time. However I dont know how to return it to the start of the while loop again.
Here is my Code:
def BubbleSort(logindata):
NoSwaps = 1
N = len(logindata)
logindata = list(logindata)
while NoSwaps == 1:
Count = 1
NoSwaps = 0
for Count in range(N-1):
if logindata[Count] > logindata[Count+1]:
temp = logindata[Count]
logindata[Count] = logindata[Count+1]
logindata[Count+1]=temp
NoSwaps=1
return tuple(logindata)
def BinarySearch(logindata,email):
First=0
Last=len(logindata)-1
ItemFound = False
SearchFailed = False
while ItemFound == False or SearchFailed == False:
Midpoint = (First + Last) // 2
if logindata[Midpoint][0] == email:
ItemFound = True
print("Email Found")
break
elif logindata[Midpoint][0] > email:
Last = Midpoint - 1
print("Not Found")
else:
First = Midpoint + 1
print("Not Found")
return False
if __name__ == "__main__":
logindata=["tom#gmail.com","Password1"],["harry#gmail.com","Password2"],["jake#gmail.com","Password3"]
logindata=BubbleSort(logindata)
print(logindata)
email=input("Enter username")
BinarySearch(logindata,email)
Just add the part you need to repeat in another while loop:
email=input("Enter username")
BinarySearch(logindata,email)
to:
while True:
email=input("Enter username")
res = BinarySearch(logindata,email)
if res:
break
print("Done")
and work with the return value of your BinarySearch function.
You also need to change the implementation of BinarySearch to return the appropriate values. Also, your condition is faulty, binary searching is ends when First <= Last, you can drop the other flags you're using:
def BinarySearch(logindata,email):
First=0
Last=len(logindata)-1
while First <= Last:
Midpoint = (First + Last) // 2
if logindata[Midpoint][0] == email:
print("Email Found")
return True
elif logindata[Midpoint][0] > email:
Last = Midpoint - 1
else:
First = Midpoint + 1
print("Not found")
return False
You shouldn't be printing Not Found whenever one of the else clause is executed, it hasn't been found yet since the search hasn't rerminated. Print that out in the end, after the while loop of BinarySearch has been exited.