python- cannot run a simple pro - python

hi im a beginner of python and linux. im using visual studio code and python 3.5.2
im trying to run something very simple unsuccess . tnx
so this is my code:
import random
def randomNumber():
n=random.randint(1,100)
print (n)
checkMatch(n)
def checkMatch(compNum):
playerNum= int(input("choose num between 1 to 100: "))
if playerNum==compNum:
print ("congratulation you won!")
elif playerNum<compNum:
print ("sorry but the num you choose is smaller then the right answer")
else:
print("sorry but the num you choose is bigger then the right answer")

The code presented has nothing which would be expected to run, as such. You define two functions, but they are never called and so nothing happens.
Try adding randomNumber() at the end, I think that then the program will work as intended.

You didn't call the function to run. Add randomNumber() to the end of your program. Were you getting an error with your if statements? Please edit your post if you were. It is working fine for me.

Related

RuntimeError: The current NumPy installation fails to pass a sanity check [duplicate]

This question already has answers here:
How do you fix "runtimeError: package fails to pass a sanity check" for numpy and pandas?
(9 answers)
Closed 1 year ago.
I recently built a new windows 10 system, and pretty much started new except for all my python files. I needed to first actually install Python, so I did (v. 3.9.1). I later installed, numpy in cmd (pip install numpy).I am currently using vscode, but I downloaded the pylint and python extension so I don't think this is the issue. I continued with my latest project, and when I went to run the code, I got an error message which looked like this:
** On entry to DGEBAL parameter number 3 had an illegal value
** On entry to DGEHRD parameter number 2 had an illegal value
** On entry to DORGHR DORGQR parameter number 2 had an illegal value
** On entry to DHSEQR parameter number 4 had an illegal value
Traceback (most recent call last):
File "c:\Users\sava\Desktop\Python\NumGuess\lvl1.py", line 2, in <module>
import numpy
File "C:\Users\sava\AppData\Local\Programs\Python\Python39\lib\site-packages\numpy\__init__.py", line 305, in <module>
_win_os_check()
File "C:\Users\sava\AppData\Local\Programs\Python\Python39\lib\site-packages\numpy\__init__.py", line 302, in _win_os_check
raise RuntimeError(msg.format(__file__)) from None
RuntimeError: The current Numpy installation ('C:\\Users\\sava\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\numpy\\__init__.py') fails to pass a sanity check due to a bug in the windows runtime. See this issue for more information:
I've never came across anything like it. I did some research but I still don't understand how to solve this issue. It probably has something to do with the installation, but I can display the code to maybe add some context:
import random
import numpy
from numlst import numlist_1
class guessnum1():
def get_number(self):
number = random.choice(numlist_1)
return number.upper()
number = random.choice(numlist_1)
print(input("guess your number, since this is the first level you need to choose a number between 1 and 10 "))
if input == number:
print("Congratulations! You got the number correct!")
else:
print("Oops, you got the number wrong you have: " + " tries " + " left")
print(str(guessnum1))
I know that my code isn't perfect, but this is my first project I am doing without any tutorials. If any of you have had this error, or know how to solve this please help me out. Thank you for reading this and happy holidays.
(edit 1: I am fairly sure it isn't the actual code that is the problem, and the numlist is from another file in my project, I didn't want to put in the other files because they are probably irrelevant to this issue, thanks)
i edit the code to this:
import random
import numpy
# from numlst import numlist_1
numlist_1 = [random.randrange(1, 50, 1) for i in range(7)]
class guessnum1():
def get_number(self):
number = random.choice(numlist_1)
return number.upper()
number = random.choice(numlist_1)
print(input("guess your number, since this is the first level you need to choose a number between 1 and 10 "))
if input == number:
print("Congratulations! You got the number correct!")
else:
print("Oops, you got the number wrong you have: " + " tries " + " left")
guessnum1
Which produces this output:
guess your number, since this is the first level you need to choose a number between 1 and 10 5
5
Oops, you got the number wrong you have: tries left
Notice that i commented out from numlst import numlist_1.
And also called the class outside of the print(str(guessnum1)) statement.
I am using python 3.9 on windows 10.
I use only the Python extenson and not pylint.
Hope this is helpful.

How to get rid of run time error on code chef

The following code works fine on both the jupiter notebook and the pycharm but its showing runtime error on codechef. The code is to check whether the permutation is ambiguous or not.
for i in range(int(input())):
n=int(input())
l=list(map(int,input().split()))
p=l[:]
for j in range(n):
p[l[j]-1]=j+1
if(p==l):
print("ambiguous")
else:
print("not ambiguous")
Welcome to Stack Overflow!
When posting, it usually helps to add more information, such as the error message itself. Regardless, I pasted your code into Python and I've deduced that the problem arises from the input() statement- I'm not sure what 'code chef' is, but it probably doesn't know that the user should input an integer into the input() prompt.
Therefore, a workaround is to hardcode your inputs as variables:
input1 = "5"
input2 = ...
for i in range(int(input1)):
n=int(input2)
...

Python print() outputs questionmarks

I am fairly new to python itself AND this forum.
I looked before if I found my problem here but haven't found a similar question before.
So I am currently working on a very very basic Tamagotchi and it has some sort of "easter egg- end credits".
It's a simple ascii (I think it's called that) copy pasta that repeats itself over and over, so it appears to be moving.
I use Pycharm, inside that if I hit Start it shows the copy pasta perfectly well.
But when I start the script outside the pycharm it only shows small Questionmark squares.
Specs:
Python 3
Pycharm
Windows 10
Hopefully someone can help me!
Also sorry if I said something wrong I am new :(!
achievement = True
while achievement == True:
clear()
print("⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⢀⣠⣶⣶⣤⡀⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⣾⣿⣿⣿⣿⣿⡞⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⣿⣿⣿⣿⣿⣿⡿⢃⡀⠄⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠘⢿⣿⣿⣿⣿⣿⣿⣿⣧⡀⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⢈⣽⣿⣿⣿⣿⣿⣿⣿⢿⣷⣦⣀⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⣸⣿⣿⣿⣿⣿⣿⣿⣿⠄⢉⣻⣿⡇⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⢠⣿⣿⡉⣀⣿⣿⣿⣿⣋⣴⣿⠟⠋⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⠄⠄⣠⣴⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣏⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⢀⣀⣼⣿⣿⣿⣿⣿⣿⠿⢿⣿⣿⣿⣿⣿⣮⡠⠄⠄⠄⠄")
print("⠄⠄⠄⠄⢰⣾⣿⣿⡿⠿⠛⠛⠛⠉⠄⠄⠄⠄⠙⠻⢿⣿⣿⣿⣶⣆⡀⠄")
print("⠄⠄⠄⠄⠄⠹⣿⣿⣦⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⢉⣿⣿⣿⣿⣿⠂")
print("⠄⠄⠄⠄⠄⠄⠈⢿⣿⣇⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⣴⣾⣿⡿⠟⠉⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⠂⢿⣿⣥⡄⠄⠄⠄⠄⢀⣠⣶⣿⣿⠟⠋⠁⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⣀⣤⣾⣿⣿⣷⣿⣃⡀⢴⣿⣿⡿⣿⣍⠄⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠈⠉⠉⠉⠉⠉⠉⠉⠄⠄⠄⠉⠙⠛⠛⠛⠛⠂⠄⠄⠄⠄⠄")
sleep(1.25)
clear()
print("⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⢀⠄⣠⣶⣶⣷⣿⣶⡊⠄⠄⣀⣤⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⣀⣴⣶⣾⢿⣿⣿⣿⣿⣿⣿⣿⣿⣶⣿⣿⡏⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⢸⣿⡍⠁⠄⠈⢿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠁⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⣼⣿⣿⣿⣿⣿⣿⣿⠏⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⣿⣿⣿⣿⣿⣿⣿⡿⠄⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⢸⣿⣿⣿⣿⣿⡿⠋⠄⠄⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠈⠻⣿⣿⣿⣿⣡⣶⣶⣄⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⣀⣀⣠⣴⣦⡤⣿⣿⣿⣿⡻⣿⣿⡯⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⡟⠄⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⢻⣿⣿⡏⠉⠙⠛⢛⣿⣿⣿⣿⠟⠄⠄⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⢿⣿⡧⠄⠄⢠⣾⣿⣿⡿⠁⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⠈⣿⣿⣄⣼⣿⣿⣿⠏⠁⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⠄⠸⡿⣻⣿⣿⣿⣿⣆⡀⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⠄⠄⣿⣻⠟⠈⠻⢿⣿⣿⣆⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⠄⠄⠿⠍⠄⠄⠄⠄⠉⠻⣿⣷⡤⣀⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠈⢻⣿⡿⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⣿⡯⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠸⠃⠄⠄⠄⠄⠄⠄⠄")
sleep(1.25)
clear()
print("⠄⠄⠄⠄⠄⠄⠄⠄⠄⢠⣴⣧⣤⣴⡖⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⠄⣰⣿⣿⣿⣿⣿⣷⣀⡀⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⠄⣿⣿⣿⣿⣿⣿⣿⣿⣷⣶⡄⠄⠄⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⠄⠈⠘⠻⢿⣿⣿⣿⣿⣿⣿⣿⣆⠄⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⣰⣿⣿⣿⣿⣿⣿⣿⣿⣿⡆⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⢤⣴⣦⣄⣀⣀⣴⣿⡟⢿⣿⡿⣿⣿⣿⣿⣿⣿⡄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠉⠉⠙⠻⠿⣿⡿⠋⠄⠈⢀⣀⣠⣾⣿⣿⣿⣿⣿⡄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⠄⢀⣠⣴⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⣶⣿⣿⣿⣿⣿⣿⣿⣿⣿⡟⠉⠋⠉⠉⠁⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⠈⠛⠛⣿⣿⣿⣿⣿⣿⣇⡀⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⢀⣠⣶⣿⣿⠿⢛⣿⣿⣿⣿⣷⣤⠄⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⣶⣷⣿⣿⡉⠄⠄⠄⠄⠉⠉⠉⠉⠉⠄⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⠘⠛⠟⢿⣤⣤⡀⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄")
sleep(1.25)
clear()
print("⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⢀⣤⣄⠄⡀⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⣴⣿⣿⣿⣿⣷⡒⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⢀⡀⣹⣿⣿⣿⣿⣿⣯⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⢀⣀⣀⣴⣿⣿⣿⣿⣿⣿⠿⠋⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⢀⣀⣤⣶⣾⠿⠿⠿⠿⣿⣿⣿⣿⣿⣿⣿⡇⠄⠄⠄⠄⠄⠄⠄")
print("⠄⡶⣶⡿⠛⠛⠉⠉⠄⠄⠄⠄⢸⣿⣿⣿⣿⣿⣿⣿⠃⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠘⠃⠄⠄⠄⠄⠄⠄⠄⠄⢠⣿⣿⣿⣿⣿⡟⠁⠄⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⠄⠄⣤⣾⣷⣿⣿⣿⣿⡏⠁⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⢀⣠⣴⣾⣿⣿⣿⣿⣿⣿⣿⣿⠂⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄")
print("⠄⢀⣤⣴⣾⣿⣿⣿⣿⡿⠛⠻⣿⣿⣿⣿⡇⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠸⣿⣿⣿⣿⠋⠉⠄⠄⠄⠄⣼⣿⣿⡿⠇⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠈⠻⣿⣿⣆⠄⠄⠄⠄⠄⣿⣿⣿⣷⡀⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠻⣿⣿⣆⡀⠄⠄⠈⠻⣿⣿⣿⣦⡄⠄⠄⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⣀⣌⣿⣿⣿⣦⡄⠄⠄⠄⠙⠻⣿⣿⣦⣀⠄⠄⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠈⠉⠉⠉⠉⠉⠁⠄⠄⠄⠄⠄⠄⠄⠘⠻⣿⢿⢖⠄⠄⠄⠄⠄")
print("⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠉⠉⠁⠄⠄⠄⠄⠄")
sleep(1.25)
clear()
and here a screenshot of what I see when I run it outside pycharm:

Getting NZEC error for python:

I am a newbie in python, started learning the language a week ago tried to test it out in codechef...
Here is the question link: https://www.codechef.com/problems/COOMILK
So with what i understood so far i typed the following piece of code..
t=int(input())
f=[]
if(t >= 1 and t <= 50):
for each in range(t):
n=int(input())
if(n >= 1 and n <=50):
m=input().split()
for i in range(len(m)):
if(m[i]=='milk'):
q=1
elif(len(m)==1 and m[0]=='cookie'):
q=0
break
elif(m[i]=='cookie' and m[i+1]!='milk'):
q=0
break
f.append(q)
for i in f:
if(i==1):
print("YES")
elif(i==0):
print("NO")
So apart from the n constraint being not checked during the input, what actually showed was a runtime error.
New to this can someone explain it clearly why i am getting the nzec error?
Compiled it on my own and for all the test cases it worked properly but does not compile on code chef.. any help would be appreciated.
What i have gathered from the net is that there is no raw_input in python 3.x that i am using currently, so just a friendly reminder here....
NZEC stands for "Non Zero Exit Code". It occurs when you encounter a runtime error usually index out of bound error.
There is a mistake in the 14th line of your code:
elif (m[i]=='cookie' and m[i+1]!='milk'):
what if i+1 is out of range?
e.g.,
Consider the input to be:
1
3
cookie milk cookie
1 is the number of testcases.
3 is the number of elements in the
list.
'cookie milk cookie' composes the list elements.
When the value of i is 2 and your code's 14th line comes to execution, it will generate an index out of bound error(because m[i+1] i.e. m[3] doesn't exist!) which is a type of runtime error and thus generates NZEC error.
Add this code after the 7th line of your code:
if m[-1]=='cookie':
f.append(0)
continue
You could try using
try:
#working code
except Exception:
pass
Try and catch exception for java code.

My python code runs a few times but as soon as I close my computer or do something else, it doesn't run again

Running this code a few times presents no issues. Upon attempting to show a friend, it doesn't work. It just hangs after the input. It's worked quite a few times before but never again unfortunately.
I've tried rewriting the code in brackets, rewriting the code to a local directory instead of the Google Drive folder I have and I've even tried rewriting from scratch in regular notepad. All this was tried in case some sort of encoding issue had occured. No such luck. I figure something is wrong with the interpreter but I'm not sure how to remedy the situation.
def bin2dec():
bin = []
a = int(input("What number are you converting to binary?: "))
while a > 0:
if a % 2 == 0:
bin.insert(0, 0)
a = a/2
elif a % 2 == 1:
bin.insert(0, 1)
a = a/2-0.5
else:
#repetition
print("Your binary equivalent is:", bin)
repeat = input("Would you like to convert another binary number?: ")
if repeat == "yes":
bin2dec()
bin2dec()
Oh....welp. It seems the problem was actually that I somehow installed two versions of pythons and I guess they had been interfering with each other. Reason I'm not deleting this Q is because I'm sure I'm not the only one who's made this mistake. However, others have probably made this mistake in an effort to ensure compatibility between versions. Bad idea.

Categories

Resources