Ok, so i'm making a mini-game sort of thing that generates a random number, whatever number it generates determines which horse has won the race. you can bet at the start and if your bet is right, you win. But my problem is when I get my bet right it always says 'Your bet lost!' I've been searching and can't find out the problem.
print ("Welcome to the Horse Race!")
print ("The horses racing are, Horse 1, Horse 2, Horse 3 and Horse 4.")
print ("who are you betting on? (Type the horse number)")
bet = input()
int(bet)
print ("The Horses are racing! who will win?")
import time
time.sleep(1)
from random import randrange
winner = randrange(1, 4)
int(winner)
if winner == 1:
print("Horse 1 has won!")
if bet == 1:
print("Your bet won!")
elif winner == 2:
print("Horse 2 has won!")
if bet == 2:
print("Your bet won!")
elif winner == 3:
print("Horse 3 has won!")
if bet == 3:
print("Your bet won!")
elif winner == 4:
print("Horse 4 has won!")
if bet == 4:
print("You bet won!")
if bet != winner:
print("Your bet lost! Sorry!")
You're casting your bet from a string to an integer, but you're not saving the casted value.
Change int(bet) to bet = int(bet) and it should work.
Note that this also applies to int(winner), although the cast isn't necessary since randrange() already returns an integer.
Related
I am making a game where if a variable is equal to one number in a range of numbers an if statement will take effect. e.g. if var1 = 4, then the if will activate but it will also give an output if var1 was to equal 5, 6, 7 or 8 for example (the numbers in a set range).
I currently have no code for this part of the game so I won't be able to add it to this question.
Sorry if this question is badly explained or too vague to answer, I am new to stack overflow. Any help would be appreciated. Thank you
Probably the best way to do this is using Swift's powerful switch statements.
You can do this:
let x = 5
switch(x){
case 0..<4:
// Will match 0-3
print("one")
case 4..<10:
// will match 4-9
print("two")
case 10...:
// will match >= 10
print("three")
default:
print("other")
}
EDIT:
If that's too robust for your situation, you can also do this:
if (0..<4).contains(x){
print("yes")
}else {
print("no")
}
or even more simply:
if x >= 0 && x < 4{
print("yes")
}else {
print("no")
}
new python programmer, looking for a fix for this issue. I'm making an autofill rubric for a school project, how would I move to a different section of my code. For example, I want to be able to re-run this function, but that breaks my input because I cannot put it into the function itself.
#TODO...
#Add students names in IN the code, same with num of students
numberOfStudents = 4
rubrick = ["Preparedness", "Engagement", "Perseverance", "Problem Solving", "Progessionalism"]
students = ["ROBERT", "DEVIN", "SKYLER", "XAVIER"]
def pickStudent():
print('Please select your student...')
for x in range(len(students)):
print(students[x])
pickStudent()
userPicked = input().upper()
if userPicked == students[0]:
print("You picked... " + students[0])
elif userPicked == students [1]:
print("You picked... " + students[1])
elif userPicked == students [2]:
print("You picked... " + students[2])
elif userPicked == students [3]:
print("You picked... " + students[3])
else:
print("Invalid user... \n")
pickStudent()
userPicked = input().upper()
Any help would be apreciated.
A few things to pick out here.
You probably want to put the user picking in a while loop to ensure it keeps asking until a correct answer is given.
You also don't need to test the entry against each item in the list in turn - instead use the in operator to see if the inputted text is found ion the list.
Combining these gives you:
while userPicked not in students:
userPicked = input().upper()
Instead of looping over students to print them, you can write neater code using join - to join all the items in the students list into a string and print that:
print('\n'.join(students))
I'm just getting started with coding and took python as my first language. I decided to do this guessing number mini-project and I've gotten this far. It works perfectly but I want the user to only input a number between 1-10 and if it exceeds that or other input is given that isn't in that range. I want to print out a text.
I have been scouring the python documentation and haven't found anything, I'm pretty sure it's something pretty simple in the conditions but I can't figure out what it is.
Also if you see any way that this code could be improved please tell me I would love to know it
def game():
mysteryNumber = random.randint(1,10)
print("I just guessed a number.")
inputByUser = input("Now choose a number from 1 to 10 : ")
chosenNumber = int(inputByUser)
if mysteryNumber == chosenNumber:
print("You guessed it right.")
elif mysteryNumber > chosenNumber:
print("Too low. Try again buddy.")
elif mysteryNumber < chosenNumber:
print("Too high. Try again buddy.")
else:
print("The number you chose ' {} ' is not a valid number.".format(chosenNumber))
Here is a picture of the full code
EDIT: Nevermind I figured it myself I added this line of code after the first if statement
elif chosenNumber > 10:
print("The number you chose ' {} ' is not a valid number.".format(chosenNumber))
game()
It's not a perfect solution as it doesn't check within the range but I'll learn more about that down the road
You could improve your if statement
elif (chosenNumber > 10) or (chosenNumber < 1):
print("The number you chose ' {} ' is not a valid number.".format(chosenNumber))
game()
I'm stuck at a point where the computer has to check for all remainders of the number which has been given by the user. Am I supposed to use a For loop for this if yes then how?
Thank you.
yes, you need to use for loop, below function will give you how you can use for loop to check whether number is prime or not.
def prime(n):
flag = 0
for i in range(2,(n/2)+2):
if n%i==0:
flag = 1
return "composite"
if flag==0:
return "prime"
def prime_or_composite(num):
div = 2
while True:
if num == div:
print("It is a prime number!")
break
elif num % div = 0:
print("It is a composite number!")
break
else:
div += 1
This works if the input number is an integer bigger than 2.
Say i have...
if (value1 == 1 || value1 == 3 || value1 == 6) {
//Things happen
}
Since it's referencing value1 each time, is there a quicker way of doing this?
Not really.
An alternative is:
switch (value1)
{
case 1:
case 3:
case 6:
// Things happen
}
But it's not "quicker"!
You can use a switch statement:
switch (value1)
{
case 1:
case 3:
case 6:
//Things happen
break;
case 4:
//Something else happens
break;
default:
//Something else happens
}
This is useful if you were otherwise going to have a lot of if statements checking the same variable.
If you mean doing something like:
if (value1 in {1, 3, 6}) ...
then no, you can't do anything like that. Objective-C doesn't have any sort of set operators for basic types. There are other ways to write your code, though, so that you can do a similar operation quickly. For example, if the number of possible values isn't too large, you can use bit positions:
if (value1 & (0x02 | 0x08 | 0x20)) ...
The compiler will probably OR those constants together at compile time, so the whole comparison takes only as long as a bitwise AND operation.