Printing in while loop, clearing previous iteration prints - validation

I have the following code:
print("Hi there!")
def inputyournumber():
while True:
num = input("your number is?:")
if int(num) <100:
break
return
The output is:
Hi there!
your number is?: 101
your number is?: 1002
your number is?: 100
your number is?: 99
i just want the initial prints: and the final output until a correct input is entered:
Hi there!
your number is?: 99
Erasing the initially wrong inputs, but keeping the prints prior the loop. Is it possible? Or do i have to reprint them? My issue in just clearing everything then reprint the prior texts together with the correct input is that it may consume more time for more complex part of the code with similar problem.

Do you mean it only print out the number you entered when the number you enter is 100?
First, the input you get, will be a string, so the comparison won't work.
maybe this is what you wanted?
def printnum():
while True:
print('Hi there!')
num = input("your number is: ")
if num != '100':
continue
break
print(num)
printnum()

Related

How to detect number in certain range

Trying to get bot to send a message when the person sends a message in range
async def on_message(message):
if 0 < Message < 100 in message.content:
await bot.message.send(message.channel, "you are in Bronze 1")
Ok first of all. Please don't code on mobile. Especially not python. That will mess things up way too much.
Second, please define your variables properly. Message is not a defined variable. It will return an error.
So, as you said, you wanted two ways to do this. The number should be the message itself, or within the message.
For the first example, all you have to do is cast the message.content to int. Then you can check if it's in the range.
if 0 < int(message.content) < 100:
...
For the second example, you will have to do something similar, however, you should split the entire message.content string and convert the number ones into integers. I'm assuming that the number will not be within a word and it will be by itself.
for word in message.content.split():
if word.isnumeric():
num = int(word)
if 0 < num < 100:
... # return and send message to avoid spamming

Error Message Trying To Use The Print Function After Using A For Loop - Total Newbie

This is now happening everytime I use the print function at the end of any for loop
I keep getting the same error message over and over.
>>> mylist = [5,10,15,20]
>>> total = 0
>>> for i in range (0,len(mylist)):
total = total + mylist[i]
print(total)
SyntaxError: invalid syntax
The error message just highlights the p in print(total) (final line of the code). I'm using the IDLE Shell 3.9.2. Has there been an update to the software? Am I doing something wrong? If I indent the final line of code to align with total = total + mylist[i], it's the only way I can get the print function to actually work. The problem is it prints as part of the for loop and I don't want it to print out 4 different outputs. I just want it to print out the sum of all the integers in the mylist variable once, which should be 50. Any help with this basic problem would be great. Also tell me how to leave a positive review and I will.
Try:
mylist = [5,10,15,20]
total = 0
For i in range (0,len(mylist)):
total = total + mylist[i]
print(total)
It will print the values.
mylist = [5,10,15,20]
total = 0
For i in mylist:
total = total + i
print(total)
It will print the values. And give you required value 50.
Just dont run these lines on python shell directly create an python file and add above code in it and run that python file.

Input to different attributes values from a random.sample list

so this is what I'm trying to do, and I'm not sure how cause I'm new to python. I've searched for a few options and I'm not sure why this doesn't work.
So I have 6 different nodes, in maya, called aiSwitch. I need to generate random different numbers from 0 to 6 and input that value in the aiSiwtch*.index.
In short the result should be
aiSwitch1.index = (random number from 0 to 5)
aiSwitch2.index = (another random number from 0 to 5 different than the one before)
And so on unil aiSwitch6.index
I tried the following:
import maya.cmds as mc
import random
allswtich = mc.ls('aiSwitch*')
for i in allswitch:
print i
S = range(0,6)
print S
shuffle = random.sample(S, len(S))
print shuffle
for w in shuffle:
print w
mc.setAttr(i + '.index', w)
This is the result I get from the prints:
aiSwitch1 <-- from print i
[0,1,2,3,4,5] <--- from print S
[2,3,5,4,0,1] <--- from print Shuffle (random.sample results)
2
3
5
4
0
1 <--- from print w, every separated item in the random.sample list.
Now, this happens for every aiSwitch, cause it's in a loop of course. And the random numbers are always a different list cause it happens every time the loop runs.
So where is the problem then?
aiSwitch1.index = 1
And all the other aiSwitch*.index always take only the last item in the list but the time I get to do the setAttr. It seems to be that w is retaining the last value of the for loop. I don't quite understand how to
Get a random value from 0 to 5
Input that value in aiSwitch1.index
Get another random value from 0 to 6 different to the one before
Input that value in aiSwitch2.index
Repeat until aiSwitch5.index.
I did get it to work with the following form:
allSwitch = mc.ls('aiSwitch')
for i in allSwitch:
mc.setAttr(i + '.index', random.uniform(0,5))
This gave a random number from 0 to 5 to all aiSwitch*.index, but some of them repeat. I think this works cause the value is being generated every time the loop runs, hence setting the attribute with a random number. But the numbers repeat and I was trying to avoid that. I also tried a shuffle but failed to get any values from it.
My main mistake seems to be that I'm generating a list and sampling it, but I'm failing to assign every different item from that list to different aiSwitch*.index nodes. And I'm running out of ideas for this.
Any clues would be greatly appreciated.
Thanks.
Jonathan.
Here is a somewhat Pythonic way: shuffle the list of indices, then iterate over it using zip (which is useful for iterating over structures in parallel, which is what you need to do here):
import random
index = list(range(6))
random.shuffle(index)
allSwitch = mc.ls('aiSwitch*')
for i,j in zip(allSwitch,index):
mc.setAttr(i + '.index', j)

Difference between random.randint and random.sample

I have read the documentation for each and find it a bit hard to comprehend the differences here. I wrote this code using random.sample:
import random
print "Welcome to the guessing game."
number_of_guesses = 1
correct_number = random.sample(range(1,101),1)
while number_of_guesses < 999:
user_choice = int(input("Guess a number between 1 and 100: "))
if user_choice == correct_number:
print "You guessed the number in %s tries." % number_of_guesses
elif user_choice < correct_number:
print "Too low, guess again."
number_of_guesses += 1
elif user_choice > correct_number:
print "Too high, guess again."
number_of_guesses += 1
When i run this code and enter a number i get "Too low, guess again." regardless of what number I enter, yet when i use random.randomint it works correctly. Can anyone explain why this happens?
random.sample(range(1,101),1) will return a singleton list containing an integer, whereas random.randint(1,100) will directly return the integer itself. Note that random.randint is an inclusive bound (i.e. it can return either of the endpoints)
You could also use random.choice(range(1, 101)) to get the integer. In any case, I'd assume that randint is the fastest option.

Python: For loops breaking issue

I'm using a for loop, and inside the for loop are two if-statements that are both being checked for each iteration of the for loop. My issue is that once one of the if-statements = True, then I want the True if-statement to stop being checked, and only check the second if-statement. Or vice versa.
print("Guess the numbers it is! Your goal is to guess a two digit number from 00-99. The two digits may be the same.")
numGuessesN = int(input("How many guesses would you like? (2, 3, or 4) "))
for numGuessesN in range (firstGuess, numGuessesN + 1) :
print("Guess number", numGuessesN)
userGuessN = int(input("What is your guess? "))
if (userGuessN == randomNum1) :
print("You've guessed the first number!")
break
else :
print("The first number is not", userGuessN)
if (userGuessN == randomNum2) :
print("You've guessed the second number!")
break
else :
print("The second number is not", userGuessN)
I know the breaks in the if-statements will completely exit the for loop, and that's not what I want. But somehow I need the for loop to "break" for only each individual statement if it turns out the statement is true, and keep checking the remaining statement until it's true, or the loop runs out of iterations.
Thanks guys! I'm new to all of this, so I'm sorry if I'm not really clear haha.
either you use nested if-statements, or (in case you have a lot, so you would have a lot of repeated code) you set a variable
first_wrong=False
if bla:
pass
else:
first_wrong=True
if bla2 and not first_wrong:
pass
if bla3 and not first_wrong:
pass

Resources