While count in python - python

user_answer = (raw_input('Can you guess the word that is missing? ')).lower()
count =0
while count <=1:
if user_answer == 'brown':
print ('Well done')
break
else:
user_answer =(raw_input('Please Guess again? '))
count+=1
else:
print ("Fail Game")
I'm working on a simple game and it allows the user to input the wrong guess three times. I have been playing around with this while loop and it works, (while count <=1) but i am a little confused as too why??? (not complaining) but can anyone explain why it works as i originally though the code should be like to one below (but these uses 5 attempts)
count = 0
while count <=3:
all rest of code is same as above.

The maximum number of inputs you can get with that code is 3. Why?
The first is outside the loop.
The second is when count is 0. After the input, count will be 1.
The third (and last) is when count is 1. After the input, count will be 2. The loop will finish in the next iteration because the condition 2 <= 1 will be False.

Related

Python How to break loop with 0

I don't understand why is not working on my code
def random_calculation(num):
return((num*77 + (90+2-9+3)))
while random_calculation:
num = int(input("Pleace enter number: "))
if num == "0":
break
else:
print(random_calculation(num))
Can you guide me what is wrong here, i really dont understand
You have several errors in your code:
You cannot do while random_calculation like this. You need to call the function, but since inside the loop you are already checking for a break condition, use while True instead.
Also, you are converting the input to int, but then comparing agains the string "0" instead of the int 0
Here's the corrected code:
def random_calculation(num):
# 90+2-9+3 is a bit strange, but not incorrect.
return((num*77 + (90+2-9+3)))
while True:
num = int(input("Please enter number: "))
if num == 0:
break
# you don't need an else, since the conditional would
# break if triggered, so you can save an indentation level
print(random_calculation(num))
so,when you start the loop it ask you what number you want to enter and then the code checks if the number is == to 0. IF the number is equal to 0: break the loop. IF the number is equal to any other number it prints the "random_calculation" function

How do I fix my (supposedly while-loop or if-statement) dice-simulator in Python so it prints the amount of tries its taken before landing on a 6?

So, I'm a little stuck on a coding assignment of mine – I'm supposed to write a code in Python simulating the throw of a dice, in which the program randomizes numbers between 1-6 until it "lands" on the number 6; the program is furthermore supposed to count the number of tries it has "thrown the dice" before "landing" on a 6, and print out print("It took", n, "tries to get a six.") in which n = the number of tries it has thrown the dice before landing on a 6.
This is what I've come up with thus far:
import random
dice = random.randint(1,6)
n = 0
while dice == 6:
n = n + 1
print("It took", n, "tries to get a 6.")
break
But, alas, it only prints out "It took 1 try to get a 6." in the result window and shows blank in the result window whenever the "dice" doesn't land on a 6. I guess what my question is, is: how do I get it to count all the tries before landing on a 6, and subsequently print the said amount of tries in combination with the statement print("It took", n, "amount of tries to get a 6.")?
The usual pattern for repeating an action an unknown number of times is to use a while True loop, and then break out of the loop when the exit condition is satisfied.
rolls = 0
while True:
die = random.randint(1, 6)
rolls += 1
if die == 6:
break
print("It took", rolls, "tries to get a 6.")
You need to write your break command into an if-statement.
In your current code it breaks within the first iteration.
As this is an assigment i dont want to tell you everything.
But that while loop condition won't get you there.
Try writing a blank while loop with a break-condition, think about it.

Is there a way to not give a point if they get it wrong the first time, quiz, python

I am new to python and am doing a basic quiz. I'm running into a problem for scoring.
trials = 3
for i in range(trials):
ans_2 = ansTwo()
results.append(ans_2)
if ans_2 in ('c','C'):
print("Good Job",name)
score+=1
break
else:
remaining = trials-i-1
s = '' if remaining == 1 else 's'
print("Oops Try Again? [{0} Chance{1} Remaining]".format(remaining, s))
This is my code and I'm trying to make it that if they do not get the answer the first time they do not get any points, but if they get the answer the first time they get a point.
Just add an if statement before score += 1 that checks if the loop is running for the first time. In your case:
if i == 0:
score += 1

Why does this code produce the error 'int is not subscriptable'?

I have converted the variable to a string, however Python still does not recognise this and says the integer is not subscriptable.
I've already tried viewing other questions with the same 'integer is not subscriptable' problem but none which answer my question specifically.
I have explicity converted the variable to a string the line before the error occurs.
import random
num = random.randint(1000, 9999)
tot_correct = 0
tot_tries = 0
while tot_correct != 4:
tot_correct = 0
tot_tries += 1
guess = input("Guess the number: ")
guess = str(guess)
#check 1st number
if guess[0] == num[0]:
tot_correct += 1
#check 2nd number
if guess[1] == num[1]:
tot_correct += 1
#check 3rd number
if guess[2] == num[2]:
tot_correct += 1
#check 4th number
if guess[3] == num[3]:
tot_correct += 1
print("You got " + tot_correct + " numbers right.")
print("You have guessed the number correctly! It took you " + tot_tries + " tries.")
I expected the string to become a string array, (but it still does not, and returns the same error) and then identify whether or not the individual number matches the one already
Your code isn't doing what you think it is. Right now you are inputting a number, converting it to a string and comparing the first character of that guess string to the first index of the number num[0] which isnt indexable.
edit:
Your code is doing a number of things wrong actually. One huge problem you have is you are setting tot_correct = 0 inside of your while loop which means it'll run forever and never finish.
But stepping back I think you are making this problem too complicated. Let's talk about the pseudocode for what I believe you are trying to do.
num_guessed = 0
number_to_guess = 4
total_guesses = 0
while num_guessed < number_to_guess:
# each pass we reset their guess to 0 and get a new random number
guess = 0
# get a new random number here
while guess != random:
# have a user guess the number here
total_guesses += 1 # we can increment their total guesses here too
# it would be a good idea to tell them if their guess is higher or lower
# when they guess it right it will end the loop
num_guessed += 1
# down here we can tell them game over or whatever you want
The code should at least give you an idea of how to approach the problem without solving it for you.
I respectfully disagree with the previous comment. It will be possible for the loop to end. I understand why you are setting tot_correct to 0 at the start of each loop. Because tot_correct is incremented up to 4 times, it is possible for tot_correct == 4 to be true.
Edit: The poster is trying to count the correct number of digits provided. So if the number to guess is '1234' and the user inputs '1564', the poster wants the code to return '2' to indicate that the '1' and '4' were correct numbers. It's like the game mastermind, where a player has to guess the correct colors and orientation of the colors. However, this code will not inform the user if a correct number is added in an incorrect position, just if the correct number is in the correct position.
However, he is correct that your error lies in your access of num[<index>]. numis an integer so you cannot index into it, hence 'integer is not subscriptable.' num needs to be a string in order to index the characters.
Edit: guess was already a string without the call to str() because the return from input() is a string
Some things to consider: Do you want your user to know they need a 4 digit number? What if there are whitespaces added? Currently your code does not remove whitespace. If you are looking for '6543' as the magic number and I enter ' 6543' your solution would not recognize my answer as correct.

Python, how do I make a countdown clock that asks the user what number they want to start at

Okays so I'm new to python and I just really need some help with this. This is my code so far. I keep getting a syntax error and I have no idea what im doing wrong
count = int(input("What number do you want the timer to start: "))
count == ">" -1:
print("count")
print("")
count = count - 1
time.sleep(1)
You need to ensure you import the time library before you can access the time.sleep method.
Also it may be more effective to a for use a loop to repeat code. The structure of your if statement is also incorrect and is not a correct expression.
IF <Expression> is TRUE:
DO THIS.
Also consider using a range within your for loop see below;
import time
count = int(input("What number do you want the timer to start: "))
for countdown in range(count,0,-1):
print (countdown)
time.sleep(1)
Explanation;
for countdown in range(count,0,-1):
range (starting point, end point, step) . Starts at your given integer, ends at 0, steps by -1 every iteration.
In the 2nd line, you can't deduct 1 from ">" which is a string.
What you need here is apparently a for loop. EDIT: You forgot the import too!
import time
count = int(input("What number do you want the timer to start: "))
for i in range(count):
print("count")
print(i)
count = count - 1
time.sleep(1)
The syntax error presumably comes from the line that reads
count == ">" -1:
I'm not sure where you got that from! What you need is a loop that stops when the counter runs out, and otherwise repeats the same code.
count = int(input("What number do you want the timer to start: "))
while count > 0:
print("count", count)
print("")
count = count - 1
time.sleep(1)
You could also replace count = count -1 with count -= 1 but that won't make any difference to the operation of the code.
First, you must import time in order to use the time.sleep() function
Next, I'm not too sure what you mean by:
count == ">" -1:
If you're creating a "stopwatch", then it would be logical to use some sort of a loop:
while count > 0:
print(count,"seconds left")
count -= 1
time.sleep(1)
print ("Finished")
That should work fine.
There is a syntax error in your second line. I am not sure what you are trying to achieve there. Probably you want to check if count>-1.
do this:
import time
count = int(input("What number do you want the timer to start: "))
if count>0:
while(count):
print(count)
time.sleep(1)
count = count -1

Categories

Resources