I am a beginner in python and It always says I have a bad input on lines 2,4, and 10 and when I put a < in front of the second line it approves it and when I put > for the 4th line it approves it and I tried it for the 10th line but it didn't work
start = int(input("type 1 to begin or 2 to buy items: "))
if start = 1 :
print("welcome to this game")
if start = 2 :
ranged = 0
melee = 1
fighting = 0
money = 100
shop = int(input("1 for sword or two for arrows: "))
if shop = 1 :
money = money-100
fighting = fighting+1
melee = melee+1
start = int(input("type 1 to begin or 2 to buy items: "))
if shop = 2 :
money = money-100
fighting = fighting+0.5
ranged = ranged+1
start = int(input("type 1 to begin or 2 to buy items: "))
else:
print("invalid selection")`
Umm so I kind of changed it a little but it still has errors on line 12 where it says it cannot recognize x but I have already defined it earlier
start = int(input('type 1 to begin or 2 to buy items: '))
if start == 1 :
print("welcome to this game")
print("you will face off with a evil monster named york for the first adventure")
print("I am york and I will eat you")
if start == 2 :
x = float(input('type 1 or 2 to buy items: '))
ranged = 0
melee = 1
fighting = 0
money = 100
if x == 1:
money = money-100
print(money)
fighting = fighting+1
melee = melee+1
start = int(input('type 1 to begin or 2 to buy items: '))
if start == 1 :
print("welcome to this game")
else:
print("visit the shop another time")
if x == 2 :
money = money-100
fighting = fighting+0.5
ranged = ranged+1
start = int(input("type 1 to begin or 2 to buy items: "))
The "=" sign in Python is for assigning a value to a variable.
Example:
>>> x = 3
>>> x+1
4
For comparison use "==".
Example:
>>> if x == 3:
... print "That is true"
...
That is true
>>> if x == 2:
... print "that is true"
...
>>>
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
as u will se in my code i have 3 items in shop and want to show customer how much he bought
but the code is not working !!!
can any one fix it for me? I cant find the exact problem in it !!!
thank u so much
apple = 0.50
orange = 0.75
banana = 0.25
m = "f"
def calculator():
m = input("Which fruit u want to purchase? \n press < a > for apple \n press < o > for orange \n press < b > for banana \n press e for exit: ")
a_p = 0
o_p = 0
b_p = 0
total = a_p + o_p + b_p
if m == "a":
a_t = eval(input("How many ? "))
a_p = apple * a_t
total = a_p + total
main()
if m == "o":
o_t = eval(input("How many ? "))
o_p = orange * o_t
total = total + o_p
main()
if m == "b":
b_t = eval(input("How many ? "))
b_p = banana * b_t
total = total + b_p
main()
else:
total = str(total)
print("You've purchased " + total + " dollar from our shop \n Have a nice day !!!")
def main():
calculator();
main()
It's because nothing happens after you enter the amounts. See here from the first if block. You call main() but then you've got nothing following it to ever print because where you print your total is in an else.
if m == "a":
a_t = eval(input("How many ? "))
a_p = apple * a_t
total = a_p + total
main() # nothing happens after this, it cannot enter the `else` block
else:
total = str(total)
print("You've purchased " + total + " dollar from our shop \n Have a nice day !!!")
You're trying to do this recursively and although that can work you're probably better off with a while loop
Example for clarity:
total = 0
product = None
while product != "e":
m = input("Which fruit u want to purchase? \n press < a > for apple \n press < o > for orange \n press < b > for banana \n press e for exit: ")
number = eval(input("How many ? "))
if m == "a":
p = apple * number
elif m == "o":
p = orange * number
total += p
product = m # when this is "e" it will break the while loop
print("You've purchased " + total + " dollar from our shop \n Have a nice day !!!") # will print after the while loop has finished
How do I solve this problem with I run in pycharm menu show but does not work?
step 1: when I press 1 this function add 1 Rikshaw or when I press 2 for the car.
step 2: now I want to know the result then press 4 to show the record but my function shows nothing
Code:
while True:
print("Press 1 For Rickshaw\n")
print("Press 2 For Car\n")
print("Press 3 For Bus\n")
print("Press 4 To Show The Record\n")
print("Press 5 To Delete The Record\n")
amount = 0
count = 0
u_ip = int(input())
if u_ip == 1:
amount = amount + 100
count = count + 1
elif u_ip == 2:
amount = amount + 200
count = count + 2
elif u_ip == 3:
amount = amout + 300
count = count + 3
elif u_ip == 4:
print("The Total amount",int(amount))
print("The Total Number of vehicle parked =",int(count))
elif u_ip == 5:
amount = 0
count = 0
else:
print("Invalid Number\n")
Your code structure is not correct.
The if-elif-else should be inside the while loop.
The amount and count variables should be defined outside of while loop because these were set to 0 in every loop (Due to this, your "4 option" showed nothing).
I recommend to print only once the usage (Outside of while loop.)
The working code:
# The following variables should be outside of while loop because these are erased in every loop.
amount = 0
count = 0
# Suggested to print the usage only once. Not in every loop.
print("Press 1 For Rickshaw")
print("Press 2 For Car")
print("Press 3 For Bus")
print("Press 4 To Show The Record")
print("Press 5 To Delete The Record")
print("press 6 to exit\n")
while True:
u_ip = int(input("Please write your option: "))
# is-elif-else should be inside the while loop!
if u_ip == 1:
amount = amount + 100
count = count + 1
elif u_ip == 2:
amount = amount + 200
count = count + 2
elif u_ip == 3:
amount = amount + 300 # Typo issue. It should be "amount"
count = count + 3
elif u_ip == 4:
print("\nThe Total amount: {}".format(int(amount)))
print("The Total Number of vehicle parked = {}\n".format(int(count)))
elif u_ip == 5:
amount = 0
count = 0
elif u_ip == 6:
break
else:
print("Invalid Number\n")
Test:
>>> python3 test.py
Press 1 For Rickshaw
Press 2 For Car
Press 3 For Bus
Press 4 To Show The Record
Press 5 To Delete The Record
press 6 to exit
Please write your option: 1
Please write your option: 2
Please write your option: 4
The Total amount: 300
The Total Number of vehicle parked = 3
Please write your option: 5
Please write your option: 4
The Total amount: 0
The Total Number of vehicle parked = 0
Please write your option: 3
Please write your option: 4
The Total amount: 300
The Total Number of vehicle parked = 3
Please write your option: 6
I am very new to python, and started learning just 1 week ago. This program works very well except when I enter a number into guess1 variable that starts with 0.
import random
import sys
def script():
while True:
number1 = random.randint(1000, 9999)
number1 = int(number1)
while True:
print ("Enter Your Guess")
guess1 = input()
guess1 = int(guess1)
while True:
if guess1 != number1:
break
elif guess1 == number1:
print ("Your Guess Was Right!")
print ("Do you want to play again? Type YES or NO")
ask = input()
ask = str(ask)
if ask == "YES" or ask == "yes":
script()
elif ask == "NO" or ask == "no":
sys.exit()
else:
print ("Invalid input, try again.")
continue
number = list(str(number1))
guess = list(str(guess1))
if len(guess) > 4:
print ("Please type a 4-digit number")
continue
bulls = 0
wr = 0
cows = 0
a = 3
while a >= 0:
if number[a] == guess[a]:
number[a] = 'a'
guess[a] = 'b'
bulls += 1
a -= 1
b = 0
c = 0
while b < 4:
c = 0
while c < 4:
if number[b] == guess[c]:
number[b] = 'a'
guess[c] = 'b'
wr += 1
c += 1
b += 1
z = bulls + wr
cows = 4 - z
bulls = str(bulls)
cows = str(cows)
wr = str(wr)
print ("Cows: "+cows)
print ("Bulls: "+bulls)
print ("Wrongly Placed: "+wr)
break
script()
This was a program written for a game, in which a 4-digit number is to be guessed. We do it by starting with a random number, and we get clues in the form of cows, bulls and wrongly placed. Cows mean the number is wrong, Bulls mean the number is right, and wrongly placed means the number is right but wrongly placed.
The whole thing works properly, but when I enter a number starting with 0, it shows something like this :-
Traceback (most recent call last):
File "GuessingGame.py", line 61, in <module>
script()
File "GuessingGame.py", line 36, in script
if number[a] == guess[a]:
IndexError: list index out of range
Please help, thanks!
UPDATE:
Thanks to user #blue_note 's answer, The program works now! This is how it has been modified -
import random
import sys
def script():
while True:
number1 = random.randint(1000, 9999)
number1 = int(number1)
while True:
print ("Enter Your Guess")
guess1 = input()
number = list(str(number1))
guess = list(str(guess1))
if guess[0] == 0:
guess1 = str(guess1)
else:
guess1 = int(guess1)
while True:
if guess1 != number1:
break
elif guess1 == number1:
print ("Your Guess Was Right!")
print ("Do you want to play again? Type YES or NO")
ask = input()
ask = str(ask)
if ask == "YES" or ask == "yes":
script()
elif ask == "NO" or ask == "no":
sys.exit()
else:
print ("Invalid input, try again.")
continue
bulls = 0
wr = 0
cows = 0
a = 3
while a >= 0:
if number[a] == guess[a]:
number[a] = 'a'
guess[a] = 'b'
bulls += 1
a -= 1
b = 0
c = 0
while b < 4:
c = 0
while c < 4:
if number[b] == guess[c]:
number[b] = 'a'
guess[c] = 'b'
wr += 1
c += 1
b += 1
z = bulls + wr
cows = 4 - z
bulls = str(bulls)
cows = str(cows)
wr = str(wr)
print ("Cows: "+cows)
print ("Bulls: "+bulls)
print ("Wrongly Placed: "+wr)
break
script()
Since my guess will always be wrong if the first digit is 0, I don't have the need to convert it into int.
Again, thanks for the help guys! This was my first question on the website. It is really a cool website.
When you pass, say, an integer starting with 0, say, 0123, and you convert it to int in the next line, you are left with 123 (3 digits). Later, you do number = list(str(number1)), so your number is ['1', '2', '3'] (length 3). Then, you try to get number[a] with a=3, and that's were you get the error.
You could do something like
number = list(str(number1) if number1 > 999 else '0' + str(number1))
I have to code the Cows and Bulls game in which I have to generate 4 random number and ask the users to guess it. I have been trying for the past few hours to code it but can't seem to come up with a solution.
The output I want is:
Welcome to cows and Bulls game.
Enter a number:
>> 1234
2 Cows, 0 Bulls.
>> 1286
1 Cows, 1 Bulls.
>> 1038
Congrats, you got it in 3 tries.
So far, I have got this:
print("Welcome to Cows and Bulls game.")
import random
def number(x, y):
cowsNbulls = [0, 0]
for i in range(len(x)):
if x[1] == y[i]:
cowsNbulls[1] += 1
else:
cowsNbulls[0] += 1
return cowsNbulls;
x = str(random.randint(0, 9999))
guess = 0
while True:
y = input("Enter a number: ")
count = number(x, y)
guess += 1
print(str(count[0]), "Cows.", str(count[1]), "Bulls")
if count[1] == 4:
False
print("Congrats, you done it in", str(guess))
else:
break;
And the output is:
Welcome to Cows and Bull game.
Enter a number: 1234
4 Cows, 0 Bulls.
It would not continue. I was just wondering what the problem is.
Try this:
print(str(count[0]), "Cows.", str(count[1]), "Bulls")
if count[0] == 4:
print("Congrats, you done it in", str(guess))
break
You want to break the while loop if the count equals 4, otherwise it should continue to run.
There are some things wrong with your code:
The while True statement has the same indent level as a function
Inside the while statement you use break which is why the statement only executes once if you fail to get the correct anwser the first time
"x & y" variables?? Please in the future use vars that make sense, not only to you, but to others
In the function "number" you have a this validation x[1] == y[i] this wont do anything, it will only compare the first char of the string
Below I made some repairs to your code, see if it's something like this that you are looking for:
import random
def number(rand_num, guess):
cowsNbulls = {
'cow': 0,
'bull': 0,
}
for i in range(len(rand_num)):
try:
if rand_num[i] == guess[i]:
cowsNbulls['cow'] += 1
else:
cowsNbulls['bull'] += 1
except:
pass
return cowsNbulls;
def game_start():
rand_number = str(random.randint(1, 9999))
tries = 0
locked = True
print("Welcome to Cows and Bulls game.")
while locked:
print(rand_number)
guess = input("Enter a number (Limit = 9999): ")
cows_n_bulls = number(rand_number, guess)
tries += 1
print(str(cows_n_bulls['cow']), "Cows.", str(cows_n_bulls['bull']), "Bulls")
if cows_n_bulls['cow'] == 4:
print("Congrats, you done it in", str(tries))
locked = False
game_start()
I have a piece of python code that is supposed to end if a variable is equal to the string 'x', but it doesnt, I dont understand why. Can someone explain please
counter = 0
total_price = 0
biggest_price = 0
smallest_price = 10000000000
house_type = 0
while house_type != "x":
house_type = input("What is the house type? ")
if house_type != "x":
number_of_rooms = int(input("How many rooms does the house have? "))
age = int(input("How old is the house? "))
price = int(input("What is the houses price? "))
if price > biggest_price :
biggest_house_type = house_type
biggest_rooms = number_of_rooms
biggest_age = age
biggest_price = price
if price < smallest_price :
smallest_house_type = house_type
smallest_rooms = number_of_rooms
smallest_age = age
smallest_house_price = price
total_price = total_price + price
counter = counter + 1
print(biggest_house_type, biggest_rooms, biggest_age, biggest_price)
print(smallest_house_price, smallest_rooms, smallest_age, smallest_price)
print(total_price / counter)
Can someone explain why the program doesn't end when X is pressed, and instead just gives house_type the value of 'x'
There were a number of issues. I tried to make a minimal working example.
counter = 0
total_price = 0
biggest_price = 0
smallest_price = 10000000000
house_type = ''
while house_type != "x":
house_type = raw_input("What is the house type? ")
if house_type != "x":
number_of_rooms = int(raw_input("How many rooms does the house have? "))
age = int(raw_input("How old is the house? "))
price = int(raw_input("What is the houses price? "))
if price > biggest_price :
biggest_price = price
if price < smallest_price :
smallest_price = price
total_price = total_price + price
counter = counter + 1
print(biggest_price)
print(smallest_price)
print(total_price / counter)
Output:
What is the house type? 3
How many rooms does the house have? 5
How old is the house? 30
What is the houses price? 100000
100000
100000
100000
What is the house type? 4
How many rooms does the house have? 2
How old is the house? 10
What is the houses price? 200000
200000
100000
150000
What is the house type? x
NOTE:
If you are on Python 2.x use raw_input(). If you are on Python 3.x use input()