Need Help determining groups on python [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I need help fixing (Or rewriting) an algorithm. It works out how many hours you have worked in a day and gives you points based on how long you work. This is what i have so far
Amount = ""
Currency = 0
print("Enter Your Figures in 24 hour clock")
print("")
import os;
from time import sleep
sleep (2)
print("You Currently Have "+Amount+" In Your Account!")
from time import sleep
print("When did you leave work?")
Left = input(": ")
print("When did you arrive at work?")
Arrival = input(": ")
Total_Hours = (int(Left) - int(Arrival))
print("900 = 9:00")
print("You worked "+Total_Hours+"")
if TH 1 < TH <=401:
print("You Have Received 4 Points!")
print("Try to work harder!")
Amount = Currency + 4
print ("Your New Balance is " + str(Amount))
I was wondering how I would put the TH into a group such as if hours worked is between 1-4 hours then output something. and if worked between 5-8 hours output something else etc. Becuase i get a syntax error at if TH **1** < TH <=401:I have found various ways on the internet before coming here but none seem to have worked. Thank you for any help you can provide

Try this:
#!/usr/bin/env python
Amount = ""
Currency = 0
print("Enter Your Figures in 24 hour clock")
print("")
print("You Currently Have " + Amount + " In Your Account!")
print("When did you leave work?")
Left = input(": ")
print("When did you arrive at work?")
Arrival = input(": ")
Total_Hours = (int(Left) - int(Arrival))
print("900 = 9:00")
print("You worked %s." % Total_Hours)
if 1 < Total_Hours <= 401:
print("You Have Received 4 Points!")
print("Try to work harder!")
Amount = Currency + 4
print("Your New Balance is " + str(Amount))
TH did not exist but Total_Hours does...
By the way you should read PEP8 (https://www.python.org/dev/peps/pep-0008/)

Just use an if-else statement instead:
if total_hours < 4:
print('You worked less than 4 hours, try to be more productive')
elif total_hours <= 8:
print('You did just enough')
else:
print('Way to go, more than 8 hours today')

Related

I'm trying to make a little age game but it closes [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm an absolute beginner at python and I am trying to make a little game where you can see in what year you'll be a certain age. The first section that calculates when you will be 100 years old works but when I come to the second part and I answer with yes it asks the questions until I fill in the current age, after that it just closes down.
This is my code:
import datetime
import time
name = input("What is your name: ") # name input
age = int(input("How old are you: ")) # age input
year1 = str((datetime.datetime.now().year - age)+100) # calculates in what year age will be 100
print(name + " will be 100 years old in the year " + year1)
answer = input("Do you want to know another age: ")
if answer == "yes":
age1 = int(input("What age do you want to know: ")) # asks for different age than 100 if answer was yes
age = int(input("How old are you: ")) # age input
year2 = str((datetime.datetime.now().year - age)+age1) # calculates in what year input age will be
print(name + " will be", age1, "years old in the year " + year2)
if answer == "no":
print("Have a nice day!")
time.sleep(5)
exit()
Can anyone tell me why it does this and how to fix it?
you can use while True and break when answer is no, like this:
import datetime
import time
name = input("What is your name: ") # name input
age = int(input("How old are you: ")) # age input
year1 = str((datetime.datetime.now().year - age)+100) # calculates in what year age will be 100
print(name + " will be 100 years old in the year " + year1)
while True:
answer = input("Do you want to know another age: ")
if answer == "yes":
age1 = int(input("What age do you want to know: ")) # asks for different age than 100 if answer was yes
age = int(input("How old are you: ")) # age input
year2 = str((datetime.datetime.now().year - age)+age1) # calculates in what year input age will be
print(name + " will be", age1, "years old in the year " + year2)
if answer == "no":
print("Have a nice day!")
break
output:
What is your name: sample
How old are you: 24
sample will be 100 years old in the year 2097
Do you want to know another age: yes
What age do you want to know: 200
How old are you: 24
sample will be 200 years old in the year 2197
Do you want to know another age: yes
What age do you want to know: 300
How old are you: 24
sample will be 300 years old in the year 2297
Do you want to know another age: no
Have a nice day!

Python: "IndentationError: unindent does not match any outer indentation level" tried to troubleshoot but doesn't work [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
When I run my code even though I see nothing wrong
Please help
def savings_tracker():
input("This program will tracker your savings progress. (Press Enter to Continue)")
goal = input("What are you trying to save for?")
final_savings_amount = float(input("What is the total amount of money needed to each your goal"))
monthly_savings = float (input("What is your monthly savings?"))
current_savings = float(input("What have you saved so far?"))
month_to_reach_goal = (final_savings_amount-current_savings)/monthly_savings
month_to_reach_goal = math.ceil(month_to_reach_goal)
print("The time it will take to get to your goal of", goal, "is", month_to_reach_goal, "months!")
print("Your getting there! Keep on saving!")
I believe this will work for you:
import math
def savings_tracker():
input("This program will tracker your savings progress. (Press Enter to Continue)")
goal = input("What are you trying to save for? ")
final_savings_amount = float(input("What is the total amount of money needed to each your goal "))
monthly_savings = float (input("What is your monthly savings ?"))
current_savings = float(input("What have you saved so far? "))
month_to_reach_goal = (final_savings_amount-current_savings)/monthly_savings
month_to_reach_goal = math.ceil(month_to_reach_goal)
print(f"The time it will take to get to your goal of {goal} is {month_to_reach_goal} months!")
print("You're getting there! Keep on saving!")
savings_tracker()
Input:
This program will tracker your savings progress. (Press Enter to Continue)
What are you trying to save for? a house
What is the total amount of money needed to each your goal 250000
What is your monthly savings? 6000
What have you saved so far? 50000
Output:
The time it will take to get to your goal of a house is 34 months!
You're getting there! Keep on saving!
It is common practice to use 4 spaces for indentation. See official recommendation (PEP 8).
That should give you something like :
def savings_tracker():
input("This program will tracker your savings progress. (Press Enter to Continue)")
goal = input("What are you trying to save for?")
final_savings_amount = float(input("What is the total amount of money needed to each your goal"))
monthly_savings = float (input("What is your monthly savings?"))
current_savings = float(input("What have you saved so far?"))
month_to_reach_goal = (final_savings_amount-current_savings)/monthly_savings
month_to_reach_goal = math.ceil(month_to_reach_goal)
print("The time it will take to get to your goal of", goal, "is", month_to_reach_goal, "months!")
print("Your getting there! Keep on saving!")
Please consider a minimal example next time since this code is quite irrelevant to this particular indentation problem.

Unable to initiate input command without while True: ?. Importance and alternates for while True:? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am trying to learn python3. Getting ahead but input commands confuse me, since they do not launch on their own? I have no formal education in the subject, besides google and youtube. I know I am missing something basic.
I've looked on www. Would appreciate a detailed explanation?
def take_input():
print (" ")
city = input("which city do you wish to travel to? \n Options are: delhi, kolkata, bombay, bengaluru : ")
city.lower()
print (" ")
days = int(input("How many days are you travelling for? eg 1 - 100 : "))
nights = days - 1
print (" ")
spending_money = int(input("How much spending money do you plan on carrying?, eg 100 - 10000 : "))
return city, days, spending_money , trip_cost(city, days, spending_money)
# trying to collect user data
#remaining code removed for minimal reproducible question :)
while True:
take_input()
while True is a basic of Python.
It means "Do This Forever".
It is important for projects that run forever and never stop.
You can break it by break.
I will talk about your code. You are using return but you are not doing anything with your returned variables. Example you can print them using print()
Example:
def take_input():
print (" ")
city = input("which city do you wish to travel to? \n Options are: delhi, kolkata, bombay, bengaluru : ")
city.lower()
print (" ")
days = int(input("How many days are you travelling for? eg 1 - 100 : "))
nights = days - 1
print (" ")
spending_money = int(input("How much spending money do you plan on carrying?, eg 100 - 10000 : "))
return city, days, spending_money , trip_cost(city, days, spending_money)
# trying to collect user data
#remaining code removed for minimal reproducible question :)
while True:
myvar = ", "
mylist = []
for i in take_input():
mylist.append(str(i))
print("Returned values:",myvar.join(mylist))

Text based game [closed]

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 6 years ago.
Improve this question
"#TextBasedGame
import random
import time
attackers=random.randint(0,100)
val1=random.randint(0,1)
health=100
bear=100
name=input("What is your name hero? ")
print("Okay ",name," i need to teach you the basics of combat")
time.sleep (1)
print ("Bear spawns")
if val1==0:
print ("Bear attacks -25 HP")
health=health -25
print ("Health = 75")
attack=int(input("Press 1 to attack "))
if attack==1:
print ("Bears HP -25")
bear = bear -25
else:
print ("Why didn't you attack?!?!?")
if bear==100:
print ("Bear =100")
else:
print ("Bear =75")"
If anyone could help me not do each health part and make it print it after every battle / attack that'd help. Please don't make it too complex this is my homework for school and i only started a few weeks ago.
You had a lot of mistakes in your code.You did not decrease the health properly and also you need to put the health in a continuous loop so that the game continues until someone dies. Here's the corrected code:
import random
import time
attackers=random.randint(0,100)
val1=random.randint(0,1)
health=100
bear=100
name=input("What is your name hero? ")
print("Okay ",name," i need to teach you the basics of combat")
time.sleep (1)
print ("Bear spawns")
while((health or bear) > 0):
if val1==0:
print ("Bear attacks -25 HP")
health -= 25
print("Health = ", health)
attack=int(input("Press 1 to attack "))
if attack==1:
print ("Bears HP -25")
bear -= 25
print("Bears HP = ", bear)
else:
print ("Why didn't you attack?!?!?")
else:
if health < bear:
print("You lost!")
else:
print("You won!")
You can print the bear's health directly by like this:
print("bear's health:", bear)
If that's what you were looking for. You're really vague...

PYTHON: A "Gambling" program. Can't change value of a variable? (v. 2.7) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
What I was trying do do was make "cash" like a balance. Therefore I want to be able to change that value. But when I run the program, the value of variable "cash" doesn't seem to change when I subtracted from it. The player is supposed to lose cash when they guess wrongly on the dice roll. More info is in the code itself. It would be much appreciated if answers have a brief explaination, im doing this to learn, its purely constructive.
print ""
import time
cash = 5000
print 'you brought','$',cash,'today'
while cash>0:
from random import randint
die = randint(1,1)
while True:
try:
print
choice1 = int(raw_input('First guess: '))
print
choice2 = int(raw_input('Second guess: '))
print
break
except ValueError:
print 'Please, enter a number.'
print 'rolling die..'
time.sleep(3)
if choice1+choice2==die:
#PROBLEM: The operation below does not change the value of cash, why not?.
cash=cash+1000
print cash
print 'you rolled',die
print 'win! you won $1000, you\'re new balance is:',cash
#PROBLEM: The new val of cash should be printed here ^ but it remains as 5000
else:
cash-1000
print 'you rolled',die
print 'lose! you lost $1000, you\'re new balance is:',cash
if cash<0:
print 'Bankrupt.'
time.sleep(3)
quit()
if cash==1000000:
print 'Millionaire!'
break
cash-1000
Here you perform a subtraction, then throw the result away. What you want instead is:
cash = cash - 1000
Or just:
cash -= 1000
cash - 1000
should be
cash -= 1000
Otherwise you aren't assigning cash - 1000 to anything; just evaluating it
In line 28, where you typed cash-1000, you are not changing the cash variable. It should be cash = cash - 1000 or simply cash -= 1000.
Other problems in your code:
Line 6: Don't import a module multiple times. This slows down the program.
Line 20: You are adding the two choices together, but I'm pretty sure you want to check both of them if they equal die. Do this by typing if choice1 == die or choice2 == die: instead. You expected cash to be printed by that line as you commented in line 26, but it never will because the if statement is flawed.

Categories