i want it so its doesnt run a script with the rest of the scripts
Weâll need a lot more information to understand what youâre asking for. What is happening right now? What do you mean ârun a scriptâ and âthe rest of the scriptsâ?
To me, running a script means typing python my_script.py
on the command line. No other scripts are getting run when I do thisâunless my_script.py
is launching new scripts as part of its code. So Iâm not sure what you are trying to do.
Hi,
if you mean that you only want to run a partial segment of your script, this is possible with PyCharm
(I am sure you may do it also with other IDEs, however).
Suppose you have the following sequence of print statements in your script for simplicity:
print('Hello')
print('Today is Thursday.')
print('Math is the language of physics.')
print('We are on the Python platform.')
If you highlight the bottom two lines, right-click, then select the following in the pop-up menu:
Execute selection in Python Console
It will only run the bottom two lines.
If you mean that you want to run only test code for a particular module without affecting other modules in a series of associated modules in your program, you can use the following line of code at the bottom of the module:
if __name__ == '__main__':
" Provide module test code here
i need so if a input is inputted it doesnt run a specific script
basically i made it so that the correct input lets you input your username
but a incorrect input says: âlicense key invalid run app againâ and then keyboard interrupts
but when input the correct input it does the same if you incorrectly input
so basically i want it so if you input a correct input then it doesnt run the incorrect input script along with it
I think we are using different definitions for these wordsâI think of a âscriptâ as being a python file, and the only input possible is to run it on the command line. It sounds like youâre talking about behavior inside the script. So what youâre saying doesnât make sense to me.
Can you show some examples of your code, and how you interact with it, and what is going wrong? Make sure to format the code according to the pinned post for this category.
ok, change script to code i guess
Have you considered dividing your script up into functions? Then at specific points you can run only the code you want to have run, and skip the stuff you donât want to run.
something like
if validate_username():
do_something()
def validate_username():
ok = False
input('enter username')
if ... : # put validation code here
ok = true
return ok
def do_something():
.... # put processing stuff here
my example is
licensekey = input("Enter License Key")
licensekey = 824
if licensekey>=824:
print("license key invalid rerun and try again")
KeyboardInterrupt
for some reason it also runs the incorrect input code
The KeyboardInterrupt
is simply the name of an exception. It doesnât raise a keyboard interrupt on its own.
To raise a KeyboardInterrupt
you need raise KeyboardInterrupt
, but itâs a strange thing to do. Using quit()
is better.
wasnt related to my question but its literally the same if i did keyboardinterrupt
>>> licensekey = input("Enter License Key")
Enter License Key12345
>>> print(licensekey)
12345
>>> licensekey = 824
>>> print(licensekey)
824
Whatever the user enters is immediately overwritten. Either remove the licensekey = 824
line, or use a different name.
licensekey = input("Enter License Key: ")
limit = 824
if licensekey >= limit:
print("license key invalid rerun and try again")
Note that you canât directly compare the result of input (which is a string) against an integer
Do you mean: âWhen I try running this code, I see the "license key invalid rerun and try again"
output, but I think I should not see thatâ?
Well, of course you see that.
The first thing that happens is that you ask for a license key.
The second thing that happens is that you ignore what was input, by replacing it with the number 824
.
The third thing that happens is that you make the comparison. As it happens, 824
is equal to 824
, therefore it is âgreater-than-or-equal-toâ (>=
) 824
. So the code inside the if
block does run.
I will make a guess that you added the licensekey = 824
because you were getting errors before. The reason for those errors is because you cannot compare a string to an integer. When you use input
, you get a string back, even if it only contains numeric digits. No automatic conversion happens. Integers are a fundamentally different kind of thing.
so i cant make it so it doesnt run it when i input a correct input
No, it means that the code as written does not currently do that.
It is still not clear to me what you want here, but your code seems to
be doing its test for âcorrectâ incorrectly.
First: you need to convert the result from input()
, which is a
string, to an integer. Your test compares against an integer, and
comparing a string to an integer is always false - the test will just
fail.
The usual way we do this in learning programmes like yours is:
value = int(input(..................))
This takes the result of input(......)
, which is a string (because it
is something the user has typed), and passes that to the function
int()
, which will accept a valid integer string, and return to
corresponding integer. And then stores the result of calling int()
in
the variable.
Now that your input value is an integer, then you can test it for
correctness.
The second issue seems to be that you, having read a value using
input()
, then ignore it and assign another value. That means that your
correctness test is never testing what the user typed, but is instead
testing the new value.
can you tell me the rewrited version with my code
licensekey = input("Enter License Key")
licensekey = 824
if licensekey>=824:
print("license key invalid rerun and try again")
KeyboardInterrupt
We try not to write your code. Weâre happy to explain things. Just
handing you working code does not help you to learn.
Have a look at my post, and then look at your code.
Also, put in some print statements.
For example, you might start by making your code be this:
licensekey = input("Enter License Key")
print( type(licensekey), repr(licensekey) )
licensekey = 824
print( type(licensekey), repr(licensekey) )
if licensekey>=824:
print("license key invalid rerun and try again")
KeyboardInterrupt
else:
print("test was false")
See if the output informs your thinking about this problem.
still doesnt work
If you mean it behaves the same: yes. The print()
calls do not change
anything. They show you the types and values of the licensekey
variable at various points. It is intended to make it possible for you
to figure out what changes might be needed to make the code behave as
you intend.
What was the output when you ran the new code?
<class âstrâ> â 824â
<class âintâ> 824
license key invalid rerun and try again