first_num = raw input ("Please input first number. ")
sec_num = raw input (" Please input second number:")
answer = into( first_num) +into( sec_num)
print " Now I will add your two numbers : ", answer
print " Pretty cool. huh ?
print " Now I'll count backwards from ",answer
counter = answer
while (counter >=0):
print counter
counter = counter-1
print " All done !"
I think the first half in a command to add first and second numbers to get sum and the second half is a command to return to start or zero out. I don't know python language.
You should probably try to run the code first and play with it to understand it. The code is simple; the first half take two user inputs and add them to each other then it displays the result.
first_num = input("Please input first number:") # get first number input
sec_num = input("Please input second number:") # get second number input
answer = int(first_num) +int(sec_num) # add up int values of the numbers
print(" Now I will add your two numbers : ", answer) # display answer
As for the second half it takes a number from which it counters downward till zero
print("Now I'll count backwards from ", answer)
counter = answer # set counter start value
while(counter >=0):
print(counter) # display counter value on each iteration
counter = counter-1 # decrement counter value on each iteration
print(" All done !)
I changed your code cause your lines were a bit messy and some incorrect. This version by me works on python3 and NOT python2.7. If you want to learn python I advise you to start with code academy python tutorial
Related
I am learning the basics of programming using Python and I am trying to compare a variable to another. Here's what I have:
code = "03"
count = 0
npw=str(input("Enter code: "))
while count != 2:
if npw == code:
print("Success")
break
else:
print("incorrect")
npw=str(input("Enter code: "))
count += 1
print("Reached Maximum Tries")
I want the user to have 3 tries to guess the code, but upon trying 3 tries, the third one was not read. Also, when I entered the correct code, it also prints the "Reached Maximum Tries".
Thanks in Advance.
You check if the code is correct before asking for the last time! Change the order around to fix that problem.
You always print "Max". You can do that using else. See this question for examples.
Minor point: you don't need to convert strings to strings!
Final solution:
code = "03"
for count in range(3):
npw = input("Enter code")
if npw == code:
print("Success")
break
print("Incorrect")
else:
print("Max")
For my assignment, I have to create a program that lets the user enter several numbers. I also want to create a list with all the numbers and their average.
But how do I create a code for the list of numbers?
I should exit with a number such as 0 or -999.
One line has invalid syntax.
print (number_list[i], end = " ")
Here is my code.
number_list = []
sum = 0.0
user_number = eval(input("Please enter a number (-999 quits): "))
# Loop until the user is ready to quit
while (user_number != -999):
number_list.append(user_number)
sum = sum + user_number
user_number = eval(input("Please enter a number (-999 quits): "))
# Make sure the user entered something
if (len(number_list) != 0):
# Compute average
average = sum / len(number_list)
# Do output
print ("Using the numbers:")
for i in range(len(number_list)):
# Note the end = " " at the end will keep the output on
# the same line
print (number_list[i], end = " ")
# Note the \n at the start of this line is needed because
# the previous print statement ended with a comma. This
# \n will move the cursor to the next line
print ("\nThe average is:", average)
else:
print ("No values were entered")
Because of Python's indentation rules, any compound statement needs to have at least one statement indented after it.
Let's focus on this section of your code:
for i in range(len(number_list)):
print (number_list[i], end = " ")
print ("\nThe average is:", average)
else:
print ("No values were entered")
A for loop is a compound statement as it needs stuff indented after it. You need to indent them accordingly. Something like:
for i in range(len(eggs)):
print(eggs[i])
The pythonic way to loop over stuff is just to use the value instead of getting the index and then finding it. Python's for loop is more like a foreach loop than an actual for loop. A remake would look like:
for spam in eggs:
print(spam)
Also, you have a check for if there aren't any numbers. Use a normal if statement for that, not one in the loop. The else behind a loop will run when the main part (while or for) finishes without a break.
This:
for spam in eggs:
print(spam)
else:
print("Nothing")
Is not the same as this:
if eggs:
for spam in eggs:
print(spam)
else:
print("Nothing")
Here's the fixed section :D
# If statement to check if list is truthy (nonempty)
if number_list:
# Note the for loop's target 'number'
for number in number_list:
# Note indentation.
print(number, end=" ")
print("\nThe average is:", average)
else:
print("No values were entered")
EDIT: You also have indentation errors after the while loop above this section. I'll leave that to you to fix :)
I am trying to get into coding and this is kinda part of the assignments that i need to do to get into the classes.
"Write a program that always asks the user to enter a number. When the user enters the negative number -1, the program should stop requesting the user to enter a number. The program must then calculate the average of the numbers entered excluding the -1."
The while loop i can do... The calculation is what im stuck on.
negative = "-1"
passable = "0"
while not passable <= negative:
passable = input("Write a number: ")
I just want to get this to work and a explanation if possible
As pointed out by some of the other answers here, you have to sum up all your answers and divide it by how many numbers you have entered.
However, remember that an input() will be a string. Meaning that our while loop has to break when it finds the string '-1' , and you have to add the float() of the number to be able to add the numbers together.
numbers=[]
while True:
ans=input("Number: ")
if ans=="-1":
break
else:
numbers.append(float(ans))
print(sum(numbers)/len(numbers))
I would initialize a list before asking the user for a number, using a do-while. Then, you add every number to that list unless the number == -1. If it does, then you sum every element in the list and output the average.
Here's pseudocode to help:
my_list = []
do
input_nb = input("Please enter a number: ")
if(input_nb != -1)
my_list.add(input_nb)
while (input_nb != -1)
average = sum(my_list) / len(my_list)
print('My average is ' + average)
You are assigning strings to your variables, which I don't think is your intention.
This will work:
next_input = 0
inputs = []
while True:
next_input = int(input('Please enter a number:'))
if next_input == -1:
break
else:
inputs.append(next_input)
return sum(inputs) / len(inputs)
First, you need to create a container to store all the entered values in. That's inputs, a list.
Next, you do need a while loop. This is another way of structuring it: a loop that will run indefinitely and a check within it that compares the current input to -1, and terminates the loop with break if it does. Otherwise, it appends that input to the list of already entered inputs.
After exiting the loop, the average is calculated by taking the sum of all the values in the entered inputs divided by the length of the list containing them (i.e. the number of elements in it).
So I basically wanna compare "Number" and "Guess" in the if statement, but no matter what it says they don't match (I get the else response, not included here). Even if I copy the random number they don't match.
Thanks in advance!
import time
def the_start():
points = 0
attempt = 1
print("Attempt:",attempt)
print("Your goal is to guess a number between 1 and 10 - Points:",points)
time.sleep(2)
attempt = attempt + 1
number = random.randint(0,10)
print(number)
guess = input("What is your guess? :")
time.sleep(2)
if guess == number:
points = points + 1
print("OMG YOU WERE RIGHT! Here, have some fake cheers! *cheer*")
time.sleep(5)
guess is a string. You need to do conversion of the string and handle error conditions. int() will convert a string to an integer, but it will throw an exception if the string is not purely numbers.
I'm trying to learn python, so I'm just writing some simple programs. I wrote these two bits of code to define two of the functions I want to use in the program, and they both do what they want but when I try to paste them into IDLE it says there is a syntax error at the second def. Any idea what this is?
here's the code:
def print_seq1(number):
number = input("Pick a number: ")
print " "
while number != 1:
if number%2==0:
print number
number = number/2
else:
print number
number = number*3 + 1
print number
print " "
choice = 0
def print_seq2(number):
number = input("Pick a number: ")
print " "
while number != 1:
if number%2==0:
print number,
number = number/2
else:
print number,
number = number*3 + 1
print number
print " "
choice = 0
Interactive interpreters (a.k.a. REPL, just "interpreter", and many other terms) usually expect only a single top-level statement (a function definition, a class definition, a global assignment, a loop, ...) at a time. You give it two and it's confused. Try putting in the first def, a blank line to confirm and actually run your input, then the second def.
When you paste, you mess up the formatting of the code, either re-indent correctly after pasting or paste functions seperately.