Home > other >  This code is what's the problem, why the output explain impassability?
This code is what's the problem, why the output explain impassability?

Time:11-22

Import the random
A=random. Randint (0, 10) if a> 2 the else 100
Print (a)

The code above idea is: to generate a random number when this number is greater than 2 output this number, otherwise the output 100
But the actual output is 100, no other results


No matter how many times the output, a value is 100, is a no less than or equal to 2? Strange

And when the code a> 2 to a> 1
Output results and is full of random Numbers, no matter how many times to refresh all have no output 100
Isn't there a less than or equal to 1?
This code where there is the problem, why the output explain impassability?

CodePudding user response:

100 - "the else else a=100

CodePudding user response:

 
# if b value greater than 0, will be directly assigned to a, b or a set to 0
If a=b b> The else 0 0

A=a> 2? Random. Randint (0, 10) : 100, don't assign a direct comparison, it doesn't offer variable does not exist error?

CodePudding user response:

The code translation whose meaning is:
 import random 
If a & gt; 2:
A=random. Randint (0, 10)
The else:
A=100
Print (a)


In principle is an error, you can run?

CodePudding user response:

refer to the second floor lkj2016 response:
 
# if b value greater than 0, will be directly assigned to a, b or a set to 0
If a=b b> The else 0 0

A=a> 2? Random. Randint (0, 10) : 100, don't assign a direct comparison, it doesn't offer variable does not exist error?


Is not an error, I have been running this line of code to read yesterday a afternoon
Results come up an error today
A face of meng!

CodePudding user response:

Short circuit assignment to determine conditions, again carries on the assignment, first to determine if a> here; 2 the else 100, here's a have assignment yet, directly to the judgment, it must be an error,

CodePudding user response:

Import the random
A=random. Randint (0, 10). If the random randint (0, 10) & gt; 2 the else 100
print(a)

CodePudding user response:

reference 4 floor Han Yang applet created response:
Quote: refer to the second floor lkj2016 response:
 
# if b value greater than 0, will be directly assigned to a, b or a set to 0
If a=b b> The else 0 0

A=a> 2? Random. Randint (0, 10) : 100, don't assign a direct comparison, it doesn't offer variable does not exist error?


Is not an error, I have been running this line of code to read yesterday a afternoon
Results come up an error today
A face of meng!


reference 4 floor Han Yang applet created response:
Quote: refer to the second floor lkj2016 response:
 
# if b value greater than 0, will be directly assigned to a, b or a set to 0
If a=b b> The else 0 0

A=a> 2? Random. Randint (0, 10) : 100, don't assign a direct comparison, it doesn't offer variable does not exist error?


Is not an error, I have been running this line of code to read yesterday a afternoon
Results come up an error today
A face of meng!


Certainly an error, estimation is not clear because of the memory to run,

CodePudding user response:

 import random 
B=random. Randint (0, 10)
If a=b b> 2 the else 100
Print (a)

CodePudding user response:

Import the random
A=random. Randint (0, 10)
If a> print (a); 2 the else 100
  • Related