Practicing some stuff and I've been way beyond this simple of a concept but this loop won't close and I can't understand why.
ans = "Y"
while ans == "Y" or "y":
num = int(input("What's your number? "))
if num % 2 == 0:
print("That number is even!\n")
else:
print("That number is odd!\n")
ans = str(input("Do you have another number, Y/y or N/n? "))
So I declare the variable first so I can enter the loop then ask for it again on the tail side to close it out...but no matter what I enter it continues.
I'm sure it's simple like I said i'm way past this kind of thing but it won't close and I'm not sure what the issue is?
The problem lies in the 'or "y"':
You should check if ans is Y or y which is correctly expressed as 'ans == "Y" or ans == "y"'
You as well needs to specify a starting condition for ans.
ans = "y"
while ans == "Y" or ans == "y":
num = int(input("What's your number? "))
if num % 2 == 0:
print("That number is even!\n")
else:
print("That number is odd!\n")
ans = str(input("Do you have another number, Y/y or N/n? "))
Related
number = ''
number = int(input("Please input a number and I will tell you if it is odd or even.\n"))
while True:
if (number % 2) != 0:
print("This is an odd number.")
else:
print("This is an even number.")
#I tried this loop below
user_input = ""
while user_input != "exit":
user_input = int(input("Please input another number and I will tell you if it is odd or even.\n"))
number = user_input
This is only telling me if the first number is odd or even but not the following numbers
Why does my code not run when option2 is 1?
option2 = ""
specificReport = "yes"
if specificReport == "yes":
specificReport = str.lower(input("Do you want to look at the specific report (yes/no) : " ))
option2 = str.lower(input("Which data are you looking for \
(1. Location, 2. Type of Risk, 3. Risk Level , 4. Date & Time): "))
if option2 == 1:
locationValue = input('Enter the location you want to inspect: ')
LocationRow = (dataframe.loc[dataframe['Location Name'] == locationValue])
print(locationRow)
Because the output of input is a string, so you compare option2 (a string) with 1 (an integer). Either you convert the integer to string (i.e., '1' instead of 1) or you convert the output of input to an integer (i.e., int(option2)).
Because it's a string. You need to make it an int
if int(option2) == 1.
Also you don't really need str.lower() on the input - you can make in int() from there too.
I have an example of a program that shows how to set up a counter for how many times each letter of the alphabet was used. I don't understand the syntax of the middle portion of the program.
LET letter$ = MID$(sentence$, LETTERNUMBER, 1)
I have tried searching on youtube and tutorials online
CLS
REM Make Counters for each Letter!!!
DIM Count(ASC("A") TO ASC("Z"))
REM Get the Sentence
INPUT "Enter Sentence:", sentence$
LET sentence$ = UCASE$(sentence$)
FOR I = ASC("A") TO ASC("Z")
LET Count(I) = 0
NEXT I
FOR LETTERNUMBER = 1 TO LEN(sentence$)
LET letter$ = MID$(sentence$, LETTERNUMBER, 1)
IF (letter$ >= "A") AND (letter$ <= "Z") THEN
LET k = ASC(letter$)
LET Count(k) = Count(k) + 1
END IF
NEXT LETTERNUMBER
PRINT
REM Display These Counts Now
LET letterShown = 0
FOR letternum = ASC("A") TO ASC("Z")
LET letter$ = CHR$(letternum)
IF Count(letternum) > 0 THEN
PRINT USING "\\## "; letter$; Count(letternum);
END IF
LET letterShown = letterShown + 1
IF letterShown = 7 THEN
PRINT
LET letterShown = 0
END IF
NEXT letternum
END
A through Z appears with the count of how many times they appeared.
The MID$ function returns a portion of a STRING's value from any position inside a string.
Syntax:
MID$(stringvalue$, startposition%[, bytes%])
Parameters:
stringvalue$
can be any literal or variable STRING value having a length. See LEN.
startposition%
designates the non-zero position of the first character to be returned by the function.
bytes%
(optional) tells the function how many characters to return including the first character when it is used.
Another method to calculate characters in a string:
REM counts and displays characters in a string
DIM count(255) AS INTEGER
PRINT "Enter string";: INPUT s$
' parse string
FOR s = 1 TO LEN(s$)
x = ASC(MID$(s$, s, 1))
count(x) = count(x) + 1
NEXT
' display string values
FOR s = 1 TO 255
PRINT s; "="; count(s); " ";
IF (s MOD 8) = 0 THEN
PRINT
IF (s MOD 20) = 0 THEN
PRINT "Press key:";
WHILE INKEY$ = "": WEND: PRINT
END IF
END IF
NEXT
END
I have been working on this for quite a bit now, I have a task of creating a program which calculates if a number entered by a user is prime number or not, the program calculates the time taken and displays this to the user, however I have found two method, one takes more time than the other but it produce accurate numbers, the other one calculates very quickly however it is wrong, I am hoping if someone can help me and tell me the quickest way of calculating this, here are my two codes
Code1:
Dim ch As String
ch = "y"
While ch = "y"
If (num Mod 2 = 0) Then
Console.WriteLine("Is not a prime number!")
Else
Console.WriteLine("Is a prime number!")
End If
Code2:
check = 1 'initilizing a check point to use it in the program to determine prime number
Dim Value As Long
Console.Write(vbLf & "Enter a number To check Whater it is Prime or Not :")
Value = Long.Parse(Console.ReadLine())
start_time = Now
Dim ch As ULong
ch = 0
Dim i As ULong
i = 2
While (i <= Value / 2)
If (Value Mod i = 0) Then
ch = 1
Exit While
End If
i = i + 1
End While
If (ch = 0) Then
Console.WriteLine("Prime Number")
Else
Console.WriteLine("Not Prime Number")
End If
There are a great many prime testers out there, many of them on this site. For checking a single number I use a faster variant of your Code2 with a little extra checking. Here is the pseudocode:
boolean function isPrime(num)
//1, 0 and negatives cannot be prime.
if (num < 2) then
return false
endif
// 2 is the only even prime.
if (num MOD 2 = 0) then
return (num = 2)
endif
// Check for odd factors.
limit <- sqrt(num)
for (factor <- 3; factor <= limit; factor <- factor + 2) do
if (num MOD factor = 0) then
return false
endif
endfor
// If we reach this point then the number is prime.
return true
endfunction
As #user448810 said, you should use the square root of your target number as the limit of your testing loop. You can basically halve the number of tests you do by treating even numbers separately. Once you have taken out the even numbers, then you only have to test odd factors: 3, 5, 7, ...
I have a Julia data frame where one column is called 'close' and I want to add another column to the data frame called 'sma' which is a simple moving average of 'close'. Thanks to anyone who can help!
I noticed a problem in the code amrod. It doesn't account for the first length of SMA that doesn't have enough previous data points for a good SMA and also gives double the SMA that is asked for. I changed it to input zeros up to that point, I also changed the variable names when I was figuring out how it works.
function makeSMA(data, SMA)
len = length(data)
y = Vector{Float64}(len)
for i in 1:SMA-1
y[i] = NaN
end
for i in SMA:len
y[i] = mean(data[i-(SMA-1):i])
end
return y
end
check this:
function ma{T <: Real}(x::Vector{T}, wind::Int)
len = length(x)
y = Vector{Float64}(len)
for i in 1:len
lo = max(1, i - wind)
hi = min(len, i + wind)
y[i] = mean(x[lo:hi])
end
return y
end
x = collect(1:100)
y = ma(x, 4)
then you can hcat(x, y).
EDIT:
If you want a backwards-looking MA you can use something like
function ma{T <: Real}(x::Vector{T}, wind::Int)
len = length(x)
y = Vector{Float64}(len)
for i in 1:len
if i < wind
y[i] = NaN
else
y[i] = mean(x[i - wind + 1:i])
end
end
return y
end